Reset PHP Array Index

I have a PHP array that looks like this:

[3] => Hello [7] => Moo [45] => America 

What does this PHP function do?

 [0] => Hello [1] => Moo [2] => America 
+90
arrays php
Sep 24
source share
3 answers

The array_values() [ docs ] function does this:

 $a = array( 3 => "Hello", 7 => "Moo", 45 => "America" ); $b = array_values($a); print_r($b); 
 Array ( [0] => Hello [1] => Moo [2] => America ) 
+218
Sep 24 '11 at 4:10
source share

Use the array_keys () function to get the array keys and the array_values ​​() function to get the array values.

You want to get the values ​​of the array:

 $array = array( 3 => "Hello", 7 => "Moo", 45 => "America" ); $arrayValues = array_values($array);// returns all values with indexes echo '<pre>'; print_r($arrayValues); echo '</pre>'; 

Exit:

 Array ( [0] => Hello [1] => Moo [2] => America ) 

You want to get the keys of an array:

 $arrayKeys = array_keys($array);// returns all keys with indexes echo '<pre>'; print_r($arrayKeys); echo '</pre>'; 

Exit:

 Array ( [0] => 3 [1] => 7 [2] => 45 ) 
0
Dec 12 '18 at 15:01
source share

If you want to reset the array key counter for any reason;

 $array1 = [ [3] => 'Hello', [7] => 'Moo', [45] => 'America' ]; $array1 = array_merge($array1); print_r($array1); 

Exit:

 Array( [0] => 'Hello', [1] => 'Moo', [2] => 'America' ) 
0
Jun 07 '19 at 4:52 on
source share



All Articles