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
- What is the difference between array_merge and array + array in PHP?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | array Functions Complete Reference
- Sort an array of dates in PHP
- PHP | Change strings in an array to uppercase
- PHP | Separate odd and even elements from array without using loop
- PHP program to find missing element(s) from an array
- PHP program to find the Standard Deviation of an array
- PHP | Print the last value of an array without affecting the pointer
- PHP | Sort array of strings in natural and standard orders
- PHP | Remove duplicate elements from Array
- PHP | Program to delete an element from array using unset() function
- PHP | Deleting an element from array using array_diff()
- PHP | Second most frequent element in an array
- PHP program to find the maximum and the minimum in array
- PHP | $_FILES Array (HTTP File Upload variables)
- Convert an object to associative array in PHP
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