array_reverse


php128 apg

RETURN an ARRAY with elements in reverse order.





If $preserve_keys = TRUE numeric keys are preserved.

Non-numeric keys are not affected by this setting and will always be preserved.



<?php

arr array_reverse 
arr $array [, bool $preserve_keys FALSE ] )
 

where,

$array The input ARRAY

$preserve_keys To control if in the returned ARRAY
                               
numeric keys will be preserved or not

?>
 

$array


The input ARRAY.



$preserve_keys


To control if the ARRAY numeric keys will be preserved or not.

VALUES MEANING DEFAULT
FALSE The numeric keys will not be preserved FALSE
TRUE The numeric keys will be preserved
ed48


  1 EXERCISE   

<?php

echo 'THE GIVEN ARRAY:<br><br>';

$arr01 = [["R""G""B"], [255255255], "WHITE"];

var_dump($arr01);

echo 
'<br><br><br>AFTER array_reverse:<br><br>';

$rarr01 array_reverse($arr01); 
// $preserve_keys = FALSE (DEFAULT)
// NUMERICAL INDICES WILL NOT BE PRESERVED

var_dump($rarr01);

?> 

 RESULT   

THE GIVEN ARRAY

array(3) { [0]=> array(3) { [0]=> string(1) "R" [1]=> string(1) "G" [2]=> string(1) "B" } [1]=> array(3) { [0]=> int(255) [1]=> int(255) [2]=> int(255) } [2]=> string(5) "WHITE" }

AFTER array_reverse

array(3) { [0]=> string(5) "WHITE" [1]=> array(3) { [0]=> int(255) [1]=> int(255) [2]=> int(255) } [2]=> array(3) { [0]=> string(1) "R" [1]=> string(1) "G" [2]=> string(1) "B" } }


  2 EXERCISE   

<?php

echo 'THE GIVEN ARRAY:<br><br>';

$arr02 = [["R""G""B"], [255255255], "WHITE"];

var_dump($arr02);

echo 
'<br><br><br>AFTER array_reverse:<br><br>';

$rarr02 array_reverse($arr02TRUE); 
// $preserve_keys = TRUE
// NUMERICAL INDICES WILL BE PRESERVED

var_dump($rarr02);

?> 

 RESULT   

THE GIVEN ARRAY

array(3) { [0]=> array(3) { [0]=> string(1) "R" [1]=> string(1) "G" [2]=> string(1) "B" } [1]=> array(3) { [0]=> int(255) [1]=> int(255) [2]=> int(255) } [2]=> string(5) "WHITE" }

AFTER array_reverse

array(3) { [2]=> string(5) "WHITE" [1]=> array(3) { [0]=> int(255) [1]=> int(255) [2]=> int(255) } [0]=> array(3) { [0]=> string(1) "R" [1]=> string(1) "G" [2]=> string(1) "B" } }


  3 EXERCISE   

<?php

echo 'THE GIVEN ARRAY:<br>'

$arr03 = array("R" => "RED"
                        
"G" => "GREEN"
                        
"B" => "BLUE"
                        
"8" => "eight");

print_r($arr03);

echo 
'<br><br><br>AFTER array_reverse:<br>';

$prsvndx03a false
// The numeric keys will not be preserved

$rarr03a array_reverse($arr03$prsvndx03a);

print_r($rarr03a);

echo 
'<br><br><br>AFTER array_reverse:<br>';

$prsvndx03b true
// The numeric keys will be preserved

$rarr03b array_reverse($arr03$prsvndx03b);

print_r($rarr03b);

?> 

 RESULT   

THE GIVEN ARRAY
Array ( [R] => RED [G] => GREEN [B] => BLUE [8] => eight )

AFTER array_reverse with $preserve_keys = FALSE
Array ( [0] => eight [B] => BLUE [G] => GREEN [R] => RED )

AFTER array_reverse with $preserve_keys = TRUE
Array ( [8] => eight [B] => BLUE [G] => GREEN [R] => RED )


  4 EXERCISE   

<?php

echo 'THE GIVEN ARRAY:<br>'

$arr04 = [ 'K' => 2'L' => 8'M' => 18
                
'N' => 32'O' => 32'P' => 18'Q' => ];   

print_r($arr04);

echo 
'<br><br><br>AFTER array_reverse:<br>';

$prsvndx04a false
// The numeric keys will not be preserved

$rarr04a array_reverse($arr04$prsvndx04a);

print_r($rarr04a);

echo 
'<br><br><br>AFTER array_reverse:<br>';

$prsvndx04b true
// The numeric keys will be preserved

$rarr04b array_reverse($arr04$prsvndx04b);

print_r($rarr04b);

?> 

  5 EXERCISE   

<?php

echo 'THE GIVEN ARRAY:<br>'

$arr05 = [ '1' => 'Brasil'
                
'2' => 'United States'
                
=> 'Japan'=> 'Portugal'];   

print_r($arr05);

echo 
'<br><br><br>AFTER array_reverse:<br>';

$prsvndx05a false
// The numeric keys will not be preserved

$rarr05a array_reverse($arr05$prsvndx05a);

print_r($rarr05a);

echo 
'<br><br><br>AFTER array_reverse:<br>';

$prsvndx05b true
// The numeric keys will be preserved

$rarr05b array_reverse($arr05$prsvndx05b);

print_r($rarr05b);

?>