computer
 






 

Question by  kioussis (24)

How do I reindex an array in php?

 
+6

Answer by  NikkoBukid (6)

Array ( [2] => Object ( [title] => Section [linked] => 1 ) [1] => Object ( [title] => Sub-Section [linked] => 1 ) [0] => Object ( [title] => Sub-Sub-Section [linked] => ) )

 
+5

Answer by  BenJohnson (28)

Removing an element from a PHP array leaves a gap, to re-index it you use the array_values() function, e.g. array_values ( array $input ) it returns an indexed array.

 
+5

Answer by  hb77 (12)

If you want your array to be numericaaly indexed, then you can use array_values(). Syntax for this is 'array array_values(array $input)'. You can use array_values($input), this will return an array with numerical indexes (like 0,1,.. number of elements in the array).

 
+5

Answer by  worker5398 (20)

If it's OK to make a new array it's this: $result = array(); foreach ( $array as $key => $val ) $result[ $key+1 ] = $val; for ( $k = count($array) ; $k-- > 0 ; ) $result[ $k+1 ] = $result[ $k ]; unset( $array[0] ); // remove the "zero" element

 
+4

Answer by  gigo (1706)

There is no explicit function for your needs. But you can use a little trick. Hold a second array in your program and serialize the content of the first one into the second array. Then delete the content of the first array and copy it back.

 
+4

Answer by  Lakshmi65 (715)

To reindex an array in PHP then it should be indexed from first or the program has to be changed based on the index. it can be reindexed by adding additional array and index for that in continuation with the main array. this is useful when the element increases so that all the element can come in the same array.

 
+1

Answer by  smiles (44)

There are several functions in php which you can reindex an array. Depending on what it is you want to the data that is contained in the array.

 
You have 50 words left!