PHP/Data Structure/array reverse

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

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

   <source lang="html4strict">

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

 </source>
   
  


Concisely reversing a string by word

   <source lang="html4strict">

<?php $reversed_s = implode(" ",array_reverse(explode(" ",$s))); ?>

 </source>
   
  


Return an array with elements in reverse order

   <source lang="html4strict">

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

 </source>
   
  


Reversing an Array Using array_reverse()

   <source lang="html4strict">

<?php

   $dogs = array("A" => "C", "B" => "D", "X" => "Z", "Q" => "T"); 
   
   array_reverse($dogs); 
printf("
%s
\n", var_export($dogs, TRUE));


   $nums = array(5, 2.2, -4, 2.3, 10); 
   array_reverse($nums); 
printf("
%s
\n", var_export($nums, TRUE));

?>

 </source>
   
  


Reversing a string by word

   <source lang="html4strict">

<?php

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

?>

 </source>