PHP Rename Array Keys in Multidimensional Array

PHP rename array keys in multidimensional array

foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = $array[$k] ['fee_id'];
unset($array[$k]['fee_id']);
}

This should work

PHP rename array keys in multidimensional array with array content

You could do something like this...

foreach ($array as $k => $v) {
$array[$v['listing ID']] = $v;
unset($array[$k]);
}

Iterate over the array. Add a new element and assign it a key using a value from the current element. Then remove the current element. Or iterate over your array and simply add new elements to some fresh array you've just instantiated.

Rename index key of multi-dimensional array php

Logic of your function is fine, but you need to pass $input parameter by reference:

function convert(&$input) {
...
}

How to replace key in multidimensional array and maintain order

This function should replace all instances of $oldKey with $newKey.

function replaceKey($subject, $newKey, $oldKey) {

// if the value is not an array, then you have reached the deepest
// point of the branch, so return the value
if (!is_array($subject)) return $subject;

$newArray = array(); // empty array to hold copy of subject
foreach ($subject as $key => $value) {

// replace the key with the new key only if it is the old key
$key = ($key === $oldKey) ? $newKey : $key;

// add the value with the recursive call
$newArray[$key] = replaceKey($value, $newKey, $oldKey);
}
return $newArray;
}

Rename array keys in multidimensional array?

Change the assignment line (line 6) around and make newname dynamic. So something like:

...foreach ($value as $key2 => $value2) {
$newname = 'new_' . $key2;
$shop[$key][$newname] = $shop[$key][$key2];
unset($shop[$key][$key2]);
}...

How to rename sub-array keys in PHP?

You could use array_map() to do it.

$tags = array_map(function($tag) {
return array(
'name' => $tag['name'],
'value' => $tag['url']
);
}, $tags);

Rename multidimensional array keys

Did you just try:

$myArray["time"] = $myArray[0];
$myArray["count"] = $myArray[1];
unset($myArray[0]);
unset($myArray[1]);

or just:

$newArray["time"] = $myArray[0];
$newArray["count"] = $myArray[1];

?

php recursive key renaming for inner arrays only

function TryRenameChildren(array $array)
{
if(isset($array['child']))
{
$array['child'] = keyprefix($array['name'], 'prefix2', $array['child']);

foreach($array['child'] as $key => $value)
if(is_array($value))
$array['child'][$key] = TryRenameChildren($value);
}

return $array;
}

$testArray = TryRenameChildren($testArray);


Related Topics



Leave a reply



Submit