Array( 0 => 'blabla',
1 => 'blabla',
2 => 'blblll' ) etc..
Is there a way to change all the numeric keys to "Name" without looping through the array (so a php function)?
Is there a way to change all the numeric keys to "Name" without looping through the array (so a php function)? |
|||||||||||||||||||||
|
If you have an array of keys that you want to use then use Given $keys = array('a', 'b', 'c', ...) and your array, $list, then do this:
List will now be array('a' => 'blabla 1', ...) etc. You have to use That's nice and simple looking but array_values makes an entire copy of the array so you could have space issues. All we're doing here is letting php do the looping for us, not eliminate the loop. I'd be tempted to do something more like:
I don't think it's all that more efficient than the first code but it provides more control and won't have issues with the length of the arrays. |
||||
|
No, there is not, for starters, it is impossible to have an array with elements sharing the same key
Secondarily, if you wish to merely prefix the numbers so that
That is computationally implausible to do without looping. No php functions presently exist for the task of "key-prefixing", and the closest thing is "extract" which will prefix numeric keys prior to making them variables. The very simplest way is this:
Additionally, upon reading XMLWriter usage, I believe you would be writing XML in a bad way.
Is not good XML.
Is better XML, because when intrepreted, the names being duplicate don't matter because they're all offset numerically like so:
|
|||||
|
|
|||||
|
I added this for an answer to another question and seemed relevant. Hopefully might help someone that needs to change the value of the keys in an array. Uses built-in functions for php.
Output:
|
|||
|
I think that he want:
|
|||||
|
The solution to when you're using |
||||
|
|
|||
|
Use array
|
||||
|
I did this for an array of objects. Its basically creating new keys in the same array and unsetting the old keys.
|
|||
|
change array key name "group" to "children".
result:
|
|||
|
You could create a new array containing that array, so:
|
|||
|