Change Array Key Without Changing Order

Change array key without changing order

Tested and works :)

function replace_key($array, $old_key, $new_key) {
$keys = array_keys($array);
if (false === $index = array_search($old_key, $keys, true)) {
throw new Exception(sprintf('Key "%s" does not exist', $old_key));
}
$keys[$index] = $new_key;
return array_combine($keys, array_values($array));
}

$array = [ 'a' => '1', 'b' => '2', 'c' => '3' ];
$new_array = replace_key($array, 'b', 'e');

how to change a key in an array while maintaining the order?

http://ideone.com/nCZnY

$array = array('a' => 1, 'd' => 2, 'c' => 3); //associative array

// rename $array['d'] as $array['b']
$array = replace_key_function($array, 'd', 'b');

var_export($array); // array('a' => 1, 'b' => 2, 'c' => 3); same order!

function replace_key_function($array, $key1, $key2)
{
$keys = array_keys($array);
$index = array_search($key1, $keys);

if ($index !== false) {
$keys[$index] = $key2;
$array = array_combine($keys, $array);
}

return $array;
}

Replace keys in an array based on another lookup/mapping array

$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);

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;
}

Change some object keys on an array and maintain order

Maybe something like this:

const data = [
{
prop1: 'Change key but keep position',
second: 'Keep same key name and position',
prop3: 'Change key but keep position',
fourth: 'Keep same key name and position',
}
];

const renameMap = { prop1: 'first', prop3: 'third'};

const res = data.map(
obj => Object.fromEntries(Object.entries(obj).map(
([key, value]) => [renameMap[key] ?? key, value]
))
);

console.log(res);

Replace key in array, with keeping order intact

A very strange question, but why not?

The following function returns nothing (a procedure) and changes the array in-place using references but feel free to rewrite it as a "real" function (without references and with a return statement somewhere).

The idea consists to search for arrays, with numeric keys and at least 2 items, in which each item has the Name and value keys. In other words, this approach doesn't care about paths where the targets are supposed to be:

function replaceKeys(&$arr) {

foreach ($arr as &$v) {
if ( !is_array($v) )
continue;

$keys = array_keys($v);

if ( count($keys) < 2 ||
$keys !== array_flip($keys) ||
array_keys(array_merge(...$v)) !== ['Name', 'value'] ) {
replaceKeys($v);
continue;
}

foreach ($v as $k => &$item) {
$item = array_combine(["Name$k", "value$k"], $item);
}
}
}

replaceKeys($list);
print_r($list);

demo

Replace Array Keys with Ascending Numbers

This will renumber the keys while preserving the order of elements.

$new_array = array_values($old_array);

replace array keys with given respective keys

array_combine(array_merge($old, $keyReplaceInfoz), $old)

I think this looks easier than what you posed.



Related Topics



Leave a reply



Submit