Hướng dẫn dùng array next trong PHP

Định nghĩa hàm next[] trong PHP

Hàm next[] trả về value của vị trí tiếp theo mà được trỏ bởi con trỏ nội tại của mảng, hoặc FALSE nếu không có phần tử nào.

Cú pháp hàm next[] trong PHP

Dưới đây là cú pháp của hàm next[] trong PHP:

next [ $array ];

Tham số

array: Bắt buộc. Xác định một mảng

Trả về giá trị

Trả về phần tử kế tiếp trong một mảng.

Ví dụ minh họa cách sử dụng hàm next[] trong PHP:

 

Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ //localhost:8080/test.php sẽ cho kết quả:

Xem thêm Hàm trong php

Hàm next[] trong PHP dùng để chuyển con trỏ nội bộ của mảng đến phần tử tiếp theo.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápnext[ $array];

Trong đó:

  • $array là mảng cần di chuyển con trỏ nội bộ.

Ví dụ

Code

$arr = array[
	'vi tri 1', 
	'vi tri 2' , 
	'vi tri 3', 
	'vi tri 4'
];
echo pos[$arr].'
'; next[$arr]; echo pos[$arr];

Kết quả

vi tri 1
vi tri 2

Như đã thấy trong ví dụ, con trỏ nội bộ đã di chuyển đến vị trí tiếp theo [ vị trí 2] so với vị trị mặc định ban đầu[ vị trí 1].

Bài viết này được đăng tại [free tuts .net]

Tham khảo: php.net

Cùng chuyên mục:

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm next[] trong PHP

Hướng dẫn cách sử dụng hàm next[] về mảng trong lập trình PHP

Nội dung chính

  • Tác dụng của hàm next[]
  • More Examples
  • Bài viết này đã giúp ích cho bạn?
  • Bài viết mới
  • 1. nextSibling javascript là gì?
  • 2. Cách dùng nextSibling trong javascript

Tác dụng của hàm next[]

The next[] function moves the internal pointer of an array to the next element, and returns its value.

The following table summarizes the technical details of this function.

Return Value:Version:
Returns the value of the next element in the array, or FALSE if there are no more elements.
PHP 4+

Tip: Every array has an internal pointer that points to the current element in the array. When a new array is created, the current pointer is initialized to reference the first element in the array.

Syntax

The basic syntax of the next[] function is given with:

The following example shows the next[] function in action.

Parameters

The next[] function accepts the following parameters.

ParameterDescription
array Required. Specifies the array to work on.

More Examples

Here're some more examples showing how next[] function actually works:

The following example demonstrates how to get the next value from an associative array:

The next[] function is commonly used along with the following functions:

  • current[] – Returns the value of the current element in an array.
  • end[] – Moves the internal pointer of an array to its last element, and returns its value.
  • prev[] – Moves the internal pointer of an array to the previous element, and returns its value.
  • reset[] – Set the internal pointer of an array to its first element, and returns its value.
  • key[] – Returns the key of the current element in an array.

Here's an example that demonstrates how these functions basically work:

Chủ Đề