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
Move All Files in a Folder to Another
PHP MySQL - Insert New Record into Table with Auto-Increment on Primary Key
How Would I Stop This Foreach Loop After 3 Iterations
MySQL Query In() Clause Slow on Indexed Column
Why Does PHP Not Complain When I Treat a Null Value as an Array Like This
Laravel and View Caching in Development -- Can't See Changes Right Away
Phpmysql Error - #1273 - #1273 - Unknown Collation: 'Utf8Mb4_General_Ci'
Magento - Redirect Customer from Observer Method
Fatal Error: Class 'Mongodb\Driver\Manager' Not Found
PHP Dom Get Nodevalue HTML? (Without Stripping Tags)
Www to Non-Www Redirect with PHP
PHP How to Remove Any Last Commas
How to Match Accented Characters with PHP Preg
Woocommerce Add to Cart Button Redirect to Checkout
Require_Once :Failed to Open Stream: No Such File or Directory
Can Closing the Browser Terminate the PHP Script on the Server