PHP/Data Structure/array reverse

Материал из Web эксперт
Перейти к: навигация, поиск

array_reverse() function reverses the order of the array elements.

//The syntax is: array array_reverse(array array)
$u = array ("A", "B", "C", "D");
$u = array_reverse ($u);

Concisely reversing a string by word

$reversed_s = implode(" ",array_reverse(explode(" ",$s)));

Return an array with elements in reverse order

$input = array ("php", 4.0, array ("green", "red"));
$result = array_reverse ($input);

Reversing an Array Using array_reverse()

    $dogs = array("A" => "C", "B" => "D", "X" => "Z", "Q" => "T"); 
    printf("<pre>%s</pre>\n", var_export($dogs, TRUE)); 
    $nums = array(5, 2.2, -4, 2.3, 10); 
    printf("<pre>%s</pre>\n", var_export($nums, TRUE)); 

Reversing a string by word

    $s = "Once upon a time there was a turtle.";
    $words = explode(" ",$s);
    $words = array_reverse($words);
    $s = implode(" ",$words);
    print $s;