Hướng dẫn dùng reverse index trong PHP

Hàm array_reverse() sẽ đảo ngược các phần tử trong mảng. Phần tử đầu tiên trở thành phần tử cuối cùng, phần tử thứ 2 thành phần tử kế cuối ... phần tử cuối cùng thành phần tử đầu tiên.

Hướng dẫn dùng reverse index 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áparray_reverse($array [,$boolean]);

Trong đó:

  • $array là mảng dữ liệu truyền vào.
  • $boolean mặc định là FALSE các cặp key => value vẫn sẽ được giữ nguyên mà chỉ đạo thứ tự. Ngược lại, nếu $boolean mang giá trị TRUE thì các khóa sẽ bị thay thế.

Ví dụ

Code

$array  = array(
	"php",
	"js",
	"css",
	"html"
);
$reversed = array_reverse($array);
$preserved = array_reverse($array, true);

echo "<pre>";
	print_r($array);
echo "</pre>";

echo "<pre>";
	print_r($reversed);
echo "</pre>";

echo "<pre>";
	print_r($preserved);
echo "</pre>";	

Kết quả

Array
(
    [0] => php
    [1] => js
    [2] => css
    [3] => html
)
Array
(
    [0] => html
    [1] => css
    [2] => js
    [3] => php
)
Array
(
    [3] => html
    [2] => css
    [1] => js
    [0] => php
)

Tham khảo:php.net

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

Hàm array_reverse() sẽ đảo ngược các phần tử trong mảng. Phần tử đầu tiên trở thành phần tử cuối cùng, phần tử thứ 2 thành phần tử kế cuối ... phần tử cuối cùng thành phần tử đầu tiên.

Cú pháp

Cú pháparray_reverse($array [,$boolean]);

Trong đó:

  • $array là mảng dữ liệu truyền vào.
  • $boolean mặc định là FALSE các cặp key => value vẫn sẽ được giữ nguyên mà chỉ đạo thứ tự. Ngược lại, nếu $boolean mang giá trị TRUE thì các khóa sẽ bị thay thế.

Ví dụ

Code

$array  = array(
	"php",
	"js",
	"css",
	"html"
);
$reversed = array_reverse($array);
$preserved = array_reverse($array, true);

echo "<pre>";
	print_r($array);
echo "</pre>";

echo "<pre>";
	print_r($reversed);
echo "</pre>";

echo "<pre>";
	print_r($preserved);
echo "</pre>";	

Kết quả

Array
(
    [0] => php
    [1] => js
    [2] => css
    [3] => html
)
Array
(
    [0] => html
    [1] => css
    [2] => js
    [3] => php
)
Array
(
    [3] => html
    [2] => css
    [1] => js
    [0] => php
)

Tham khảo:php.net

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm array_reverse() trong PHP

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

Tác dụng của hàm array_reverse()

The array_reverse() function reverses the order of the elements in an array.

The following table summarizes the technical details of this function.

Return Value:Returns the reversed array.
Version:PHP 4+

Syntax

The basic syntax of the array_reverse() function is given with:

array_reverse(array, preserve);

The following example shows the array_reverse() function in action.

<?php
// Sample array
$fruits = array("apple", "banana", "orange", "mango");

// Reversing the order of the array
print_r(array_reverse($fruits));
?>

Parameters

The array_reverse() function accepts the following parameters.

ParameterDescription
array Required. Specifies the array to work on.
preserve Optional. Specifies whether numeric keys should be preserved or not. Possible values are true and false. Non-numeric keys will always be preserved.

More Examples

Here're some more examples showing how array_reverse() function actually works:

The following example shows how to reverse the order of array elements while preserving the keys.

<?php
// Sample array
$input = array("dog", "zebra", array("cat", "tiger"));

// Reversing the order of the array
$reversed = array_reverse($input);
$preserved = array_reverse($input, true);

// Printing arrays
print_r($input);
print_r($reversed);
print_r($preserved);
?>

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

Bài viết mới