I'm trying to change the keynames of the keys in my arraylist. Say, I have an array like the one below. I want to change "n" to "name" and "l" to "surname"
Array
(
[0] => Array
(
[n] => Amy
[l] => Gonzalez
)
[1] => Array
(
[n] => Jeff
[l] => Garcia
)
)
This is my code. $data is where the array is stored and I get it dynamically.
$newkey = array('n'=>'name','l'=>'surname');
$count = count($data);
$i=0;
if($i<=$count){
foreach( $data as &$item )
{
foreach( $newkey as $key => $replace )
{
if (key_exists($key,$item))
{
$item[$replace] = $item[$key];
unset($item[$key]);
}
}
}
$i++;
}
This works for the last entry only. What am I doing wrong? My array may sometimes fetch several results, not only 2.
$iand$countdoing? Can you remove those and leave just theforeach?