How to re-index an array in PHP?

The re-index of an array can be done by using some inbuilt function together. These functions are:

  • array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of second array will be the values of this new array.
  • range() Function: The range() function is an inbuilt function in PHP which is used to create an array of elements of any kind such as integer, alphabets within a given range (from low to high) i.e, list’s first element is considered as low and last one is considered as high.
  • count() Function: The count() function is used to count the current elements in the array. The function might return 0 for the variable that has been set to an empty array. Also for the variable which is not set the function returns 0.
  • array_values() Function: This function is used to get an array of values from another array that may contain key-value pairs or just values. The function creates another array where it stores all the values and by default assigns numerical keys to the values.

We will use array_values() function to get all the values of the array and range() function to create an array of elements which we want to use as new keys or new index of the array (reindexing). Then the array_combine() function will combine both the array as keys and values.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare an associative array
$arr = array (
    0 => 'Tony',
    1 => 'Stark',
    2 => 'Iron'
    3 => 'Man' 
);
  
echo "Array before re-indexing\n";
  
// Using foreach loop to print array elements
foreach( $arr as $key => $value) { 
    echo "Index: " . $key . " Value: " . $value . "\n"
}
  
// Set the index number from three
$New_start_index = 3;
  
$arr = array_combine(range($New_start_index
                count($arr) + ($New_start_index-1)),
                array_values($arr));
  
echo "\nArray after re-indexing\n";
  
// Using foreach loop to print array elements
foreach( $arr as $key => $value) { 
    echo "Index: ".$key." Value: ".$value."\n"
}
  
?>

chevron_right


Output:

Array before re-indexing
Index: 0 Value: Tony
Index: 1 Value: Stark
Index: 2 Value: Iron
Index: 3 Value: Man

Array after re-indexing
Index: 3 Value: Tony
Index: 4 Value: Stark
Index: 5 Value: Iron
Index: 6 Value: Man

Example 2: Add some data at the beginning of the array and then slice the array from the index.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare an associative array
$arr = array (
    0 => 'Tony',
    1 => 'Stark',
    2 => 'Iron'
    3 => 'Man'
);
  
echo "Array before re-indexing\n";
  
// Using foreach loop to print array elements
foreach( $arr as $key => $value) { 
    echo "Index: " . $key . " Value: " . $value . "\n"
}
  
// Set the index number from three
$New_start_index = 3;
  
$raw_data = range( 0, $New_start_index-1 );
  
// Add data to the start of the array
foreach( $raw_data as $key => $value) {
    array_unshift($arr, $value);
}
  
  
$arr = array_values($arr);
  
// Remove the unnecessary index so we start at 10
$arr = array_slice($arr, $New_start_index, count($arr), true);
echo "\nArray after re indexing\n";
  
// Using foreach loop to print array 
foreach( $arr as $key => $value) { 
    echo "Index: ".$key." Value: ".$value."\n"
}
  
?>

chevron_right


Output:

Array before re-indexing
Index: 0 Value: Tony
Index: 1 Value: Stark
Index: 2 Value: Iron
Index: 3 Value: Man

Array after re indexing
Index: 3 Value: Tony
Index: 4 Value: Stark
Index: 5 Value: Iron
Index: 6 Value: Man

In this example, first add some data to the array and for that again we are doing this with the help of loop and then remove the data which we added so it is also not a good choice to re-index array. This method is not suitable for re-index alphabetical keys.

Example 3: This example re-index the array from alphabet ‘p’. Two extra functions are used to re-index the alphabets which are:

  • ord() Function: The ord() function is an inbuilt function in PHP that returns the ASCII value of the first character of a string.
  • chr() Function: The chr() function is an built-in function in PHP which is used to convert a ASCII value to a character.
filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Declare an associative array
$arr = array
    'a' => 'India',
    'b' => 'America',
    'c' => 'Russia',
    'd' => 'China'
);
  
echo "Array before re-indexing\n";
  
// Using foreach loop to print array elements
foreach( $arr as $key => $value) { 
    echo "Index: " . $key . " Value: " . $value . "\n"
}
  
// Set the index from 'p'
$New_start_index = 'p';
  
// The ord() function is used to get ascii value
// The chr() function to convert number to ASCII char
$arr = array_combine(range($New_start_index
            chr(count($arr) + (ord($New_start_index)-1))),
            array_values($arr));
  
echo "\nArray after re indexing\n";
  
// Using foreach loop to print array 
foreach( $arr as $key => $value) { 
    echo "Index: " . $key . " Value: " . $value . "\n"
}
  
?>

chevron_right


Output:

Array before re-indexing
Index: a Value: India
Index: b Value: America
Index: c Value: Russia
Index: d Value: China

Array after re indexing
Index: p Value: India
Index: q Value: America
Index: r Value: Russia
Index: s Value: China


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai