Reset keys of array elements using PHP ?

PHPServer Side ProgrammingProgramming

To reset keys of array elements using PHP, the code is as follows−

Example

 Live Demo

<?php
   $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");
   var_dump ($arr);
   $res = array_values($arr);
   var_dump ($res);
?>

Output

This will produce the following output−

array(4) {
   ["p"]=>
   string(3) "150"
   ["q"]=>
   string(3) "100"
   ["r"]=>
   string(3) "120"
   ["s"]=>
   string(3) "110"
}
array(4) {
   [0]=>
   string(3) "150"
   [1]=>
   string(3) "100"
   [2]=>
   string(3) "120"
   [3]=>
   string(3) "110"
}

Example

Let us now see another example −

 Live Demo

<?php
   $arr = array( 8=>"Ben", 4=>"Kevin", 7=>"Mark", 3=>"Hanks");
   var_dump ($arr);
   $res = array_values($arr);
   var_dump ($res);
?>

Output

This will produce the following output−

array(4) {
   [8]=>
   string(3) "Ben"
   [4]=>
   string(5) "Kevin"
   [7]=>
   string(4) "Mark"
   [3]=>
   string(5) "Hanks"
}
array(4) {
   [0]=>
   string(3) "Ben"
   [1]=>
   string(5) "Kevin"
   [2]=>
   string(4) "Mark"
   [3]=>
   string(5) "Hanks" 
}
raja
Published on 27-Dec-2019 08:00:05
Advertisements