Well let me tell you that there are more than 1 ways of changing the key name of an array. Here I will discuss the ones that can be useful in some situations. If you have any question or suggestion, do let me know.
Changing Key name of a single entry of an Array
The scenario is:
We have an array which have a key with some value like this:
$ourarray['oldkey'] = "it's value";
We want to change the ‘oldkey’ to ‘newkey’. Now this can be done in two ways easily. One is to simply remove it and add a new key in the array with this key’s value. But this will bring this key to the end of an array, which we might not want in some situation. The best way to do it is then to use the following code:
array_change_key_name( 'oldkey', 'newkey', $ourarray );
This will change the name of the key in it’s own place with the same value!
You might also like
- [Solved]How to create a HTML table from PHP array
- Simple AJAX post() function
- beforeSend for .post in AJAX
- PHP mail function (for sending $_POST or $_GET data from form or AJAX)
- [Solved] #1063 – Incorrect column specifier for column . . .