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:

";

   $mode = next($transport);
   print "$mode 
"; $mode = current($transport); print "$mode
"; $mode = prev($transport); print "$mode
"; $mode = end($transport); print "$mode
"; $mode = current($transport); print "$mode
"; ?>

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ỉ http://localhost:8080/test.php sẽ cho kết quả:

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

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.

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

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:Returns the value of the next element in the array, or FALSE if there are no more elements.
Version: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:

"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");

// Getting the element's values
echo current($alphabets); // Prints: apple
echo next($alphabets);    // Prints: ball

// Getting the next element's key
echo key($alphabets); // Prints: b
?>

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:

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Trong bài này chúng ta sẽ tìm hiểu thuộc tính nextSibling trong javascript, qua đó bạn sẽ biết cách lấy node kế tiếp node hiện tại trong javascript.

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

Nếu bạn sử dụng jQuery thì ít khi quan tâm đến nextSibling. Nhưng nếu bạn code javascript thuần thì đây là một thuộc tính khá hay và hữu ích.

1. nextSibling javascript là gì?

nextSibling là một thuộc tính của các node javascript, công dụng của nó là trả về node kế tiếp node hiện tại (node đang được chỉ định). Cú pháp của nó như sau:

nextNode = node.nextSibling

Trong đó node chính là node được chỉ định, còn nextNode chính là node kế tiếp được trả về từ thuộc tính nextSibling.

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

2. Cách dùng nextSibling trong javascript

Trong cấu trúc DOM của javascript có nhiều loại node khác nhau, điển hình là:

  • Node #element - chính là các thẻ html
  • Node #text - chính là các dòng xuống hàng
  • Node #comment - là các dòng comment trong html

Khi bạn enter xuống hàng thì nó sẽ tạo ra một node #text. Xem ví dụ dưới đây:

Value 1
Value 2

Nhưng khi bạn để chúng cùng một hàng thì nó lại là node span value2.

Value 1Value 2

Trên là cách dùng thuộc tính nextSibling trong javascript.