Open In App

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:

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:




<?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"
}
  
?>

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.




<?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"
}
  
?>

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:




<?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"
}
  
?>

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

Article Tags :