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.
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.
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.
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
- Array.BinarySearch(Array, Object) Method with examples in C#
- What is the difference between array_merge and array + array in PHP?
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- How to sort an Array in C# | Array.Sort() Method Set – 2
- Difference between array() and  in PHP
- Kotlin Array
- How to get the first element of an array in PHP?
- How to get random value out of an array in PHP?
- PHP | array() Function
- PHP append one array to another
- C# | Array vs ArrayList
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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