419

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This is fine for most instances, however I want a function that gets the human-readable name of the array and uses that for the key, without changing the value.

I didn't see a function that does this, but I'm assuming I need to provide the old key and new key (both of which I have) and transform the array. Is there an efficient way of doing this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431

25 Answers25

664
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
KernelM
  • 8,776
  • 2
  • 23
  • 16
  • 7
    Just be careful that 1) No two keys have the same human-readable version 2) No human-readable versions happen to be numbers – Greg Oct 27 '08 at 17:38
  • 94
    Also this would presumably change the order of the array, which you may need to be careful of. Even associative arrays in PHP are ordered, and sometimes that order is leveraged. – Robin Winslow Feb 09 '12 at 17:14
  • 8
    Yeah, great point Robin. Is there a way to keep the same order? Or do you need to create a new array to achieve that? – Simon East Jun 28 '12 at 01:19
  • 45
    Bonus question: How to change the ID, but preserve array order? – Petr Peller Dec 11 '12 at 22:45
  • 20
    if the key value is not changing you will delete an array element. You might want to check for it. – Peeech Mar 10 '13 at 12:19
  • 1
    Tom Ritter's answer preserves the order. – Jorge Orpinel Pérez Apr 10 '14 at 15:32
  • 1
    There is also a problem that the new key may overwrite an existing old key and its values which leads to deleting the old key item from an array. In that case, you need to use two arrays. – TeeJay Jul 29 '15 at 14:41
  • I can't reply comment to Petr Peller's question (that want to change key but preserve the array order) because my reputation is not enough. I found it in... http://stackoverflow.com/questions/10182684/how-to-change-a-key-in-an-array-while-maintaining-the-order maybe useful :) – Mei Jun 06 '14 at 03:31
  • this will unset the value as well, so not a good option for assotiative arrat – Giedrius May 22 '17 at 08:46
  • 1
    Assign by reference to avoid copying the value: $arr[$newkey] = &$arr[$oldkey]; – andres101 Jan 17 '19 at 10:29
  • Doesn't this change the value inside that key instead? – S. Dre Feb 17 '22 at 13:14
120

The way you would do this and preserve the ordering of the array is by putting the array keys into a separate array, find and replace the key in that array and then combine it back with the values.

Here is a function that does just that:

function change_key( $array, $old_key, $new_key ) {

    if( ! array_key_exists( $old_key, $array ) )
        return $array;

    $keys = array_keys( $array );
    $keys[ array_search( $old_key, $keys ) ] = $new_key;

    return array_combine( $keys, $array );
}
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • 3
    Thanks this was really helpful as I did need to preserve the order of the array. I had already tried the accepted answer before I found this page. – gillytech Feb 02 '16 at 07:46
  • 4
    Yes much prefer preserving the order of the array, looks neater. – Phil Cook Mar 24 '16 at 12:01
  • Mind if you want performances or order preservation: https://stackoverflow.com/a/58619985/1617857 – Léo Benoist Oct 30 '19 at 06:55
  • 1
    This is very bad implementation, performance wise. It's better to preserve order separately, or to use ```array_splice``` like in this example: https://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php – Milos Radojevic Aug 19 '21 at 08:27
60

if your array is built from a database query, you can change the key directly from the mysql statement:

instead of

"select ´id´ from ´tablename´..."

use something like:

"select ´id´ **as NEWNAME** from ´tablename´..."
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Simon Franco
  • 625
  • 5
  • 2
  • 1
    Rather than sidestepping this task and assuming that the input data is coming from a result set, perhaps you should have sought this clarification before posting an answer. – mickmackusa Feb 26 '22 at 07:54
  • 1
    @Conrado I understand how this answer serves your individual need, but it is not good for a bloated Stack Overflow page to be extended with hypotheticals because it slows down the researcher experience. Please read my answer to understand why many answers on this page "missed the mark" regarding the actual question requirements. – mickmackusa Jan 13 '23 at 22:05
22

The answer from KernelM is nice, but in order to avoid the issue raised by Greg in the comment (conflicting keys), using a new array would be safer

$newarr[$newkey] = $oldarr[$oldkey];
$oldarr=$newarr;
unset($newarr);
kjg
  • 268
  • 2
  • 6
  • This is a good solution, so long as your array is of a reasonable size. If your array consumes more than half of available PHP memory, this will not work. – kingjeffrey Mar 08 '11 at 00:51
  • 14
    @kingjeffrey, not really. Array values will not be duplicated as long as they are "just copied" without being modified. E.g., if there's one array that contains 10'000 elements and consumes 40MB memory, copying it will consume memory that's needed for storing 10'000 only *references to already existing values* rather than *copies of values*, so if 1 array consumes 40MB, its copy might consume maybe 0.5MB (tested). – binaryLV Jul 19 '11 at 06:56
19
$array = [
    'old1' => 1
    'old2' => 2
];

$renameMap = [
    'old1' => 'new1',   
    'old2' => 'new2'
];

$array = array_combine(array_map(function($el) use ($renameMap) {
    return $renameMap[$el];
}, array_keys($array)), array_values($array));

/*
$array = [
    'new1' => 1
    'new2' => 2
];
*/
temuri
  • 2,767
  • 5
  • 41
  • 63
  • 2
    Nice and tight. If you are working on a large array and don't want to change all the keys, the line in the map function becomes `return isset($renameMap[$el]) ? $renameMap[$el] : $el;` – Jerry Apr 01 '21 at 23:12
  • 1
    This answer is missing its educational explanation. – mickmackusa Feb 26 '22 at 07:57
17

You could use a second associative array that maps human readable names to the id's. That would also provide a Many to 1 relationship. Then do something like this:

echo 'Widgets: ' . $data[$humanreadbleMapping['Widgets']];
Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
14

Simple benchmark comparison of both solution.

Solution 1 Copy and remove (order lost, but way faster) https://stackoverflow.com/a/240676/1617857

<?php
$array = ['test' => 'value', ['etc...']];

$array['test2'] = $array['test'];
unset($array['test']);

Solution 2 Rename the key https://stackoverflow.com/a/21299719/1617857

<?php
$array = ['test' => 'value', ['etc...']];

$keys = array_keys( $array );
$keys[array_search('test', $keys, true)] = 'test2';
array_combine( $keys, $array );

Benchmark:

<?php
$array = ['test' => 'value', ['etc...']];


for ($i =0; $i < 100000000; $i++){
    // Solution 1
}


for ($i =0; $i < 100000000; $i++){
    // Solution 2
}

Results:

php solution1.php  6.33s  user 0.02s system 99% cpu 6.356  total
php solution1.php  6.37s  user 0.01s system 99% cpu 6.390  total
php solution2.php  12.14s user 0.01s system 99% cpu 12.164 total
php solution2.php  12.57s user 0.03s system 99% cpu 12.612 total
Léo Benoist
  • 2,511
  • 1
  • 20
  • 18
  • Your benchmark doesn't seem correct. You are only changing the first element 100 million times which after first time, no changes are done. I think if benchmark change multiple keys second solution would be faster, since all the operation is done on C++ side with no reverting to PHP in between. – AaA Dec 30 '22 at 04:43
13

If you want also the position of the new array key to be the same as the old one you can do this:

function change_array_key( $array, $old_key, $new_key) {
    if(!is_array($array)){ print 'You must enter a array as a haystack!'; exit; }
    if(!array_key_exists($old_key, $array)){
        return $array;
    }

    $key_pos = array_search($old_key, array_keys($array));
    $arr_before = array_slice($array, 0, $key_pos);
    $arr_after = array_slice($array, $key_pos + 1);
    $arr_renamed = array($new_key => $array[$old_key]);

    return $arr_before + $arr_renamed + $arr_after;
}
spreadzz
  • 270
  • 3
  • 10
7

If your array is recursive you can use this function: test this data:

    $datos = array
    (
        '0' => array
            (
                'no' => 1,
                'id_maquina' => 1,
                'id_transaccion' => 1276316093,
                'ultimo_cambio' => 'asdfsaf',
                'fecha_ultimo_mantenimiento' => 1275804000,
                'mecanico_ultimo_mantenimiento' =>'asdfas',
                'fecha_ultima_reparacion' => 1275804000,
                'mecanico_ultima_reparacion' => 'sadfasf',
                'fecha_siguiente_mantenimiento' => 1275804000,
                'fecha_ultima_falla' => 0,
                'total_fallas' => 0,
            ),

        '1' => array
            (
                'no' => 2,
                'id_maquina' => 2,
                'id_transaccion' => 1276494575,
                'ultimo_cambio' => 'xx',
                'fecha_ultimo_mantenimiento' => 1275372000,
                'mecanico_ultimo_mantenimiento' => 'xx',
                'fecha_ultima_reparacion' => 1275458400,
                'mecanico_ultima_reparacion' => 'xx',
                'fecha_siguiente_mantenimiento' => 1275372000,
                'fecha_ultima_falla' => 0,
                'total_fallas' => 0,
            )
    );

here is the function:

function changekeyname($array, $newkey, $oldkey)
{
   foreach ($array as $key => $value) 
   {
      if (is_array($value))
         $array[$key] = changekeyname($value,$newkey,$oldkey);
      else
        {
             $array[$newkey] =  $array[$oldkey];    
        }

   }
   unset($array[$oldkey]);          
   return $array;   
}
pajafumo
  • 91
  • 1
  • 2
6

Here is a helper function to achieve that:

/**
 * Helper function to rename array keys.
 */
function _rename_arr_key($oldkey, $newkey, array &$arr) {
    if (array_key_exists($oldkey, $arr)) {
        $arr[$newkey] = $arr[$oldkey];
        unset($arr[$oldkey]);
        return TRUE;
    } else {
        return FALSE;
    }
}

pretty based on @KernelM answer.

Usage:

_rename_arr_key('oldkey', 'newkey', $my_array);

It will return true on successful rename, otherwise false.

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 1
    Be aware that this alters the order of the array (the renamed key's element will be at the end of the array, not in the same position in the array as it originally was). Also I wouldn't usually start a function name with an underscore (that's traditionally used to designate special internal use functions). – orrd Jun 18 '16 at 01:30
6

I like KernelM's solution, but I needed something that would handle potential key conflicts (where a new key may match an existing key). Here is what I came up with:

function swapKeys( &$arr, $origKey, $newKey, &$pendingKeys ) {
    if( !isset( $arr[$newKey] ) ) {
        $arr[$newKey] = $arr[$origKey];
        unset( $arr[$origKey] );
        if( isset( $pendingKeys[$origKey] ) ) {
            // recursion to handle conflicting keys with conflicting keys
            swapKeys( $arr, $pendingKeys[$origKey], $origKey, $pendingKeys );
            unset( $pendingKeys[$origKey] );
        }
    } elseif( $newKey != $origKey ) {
        $pendingKeys[$newKey] = $origKey;
    }
}

You can then cycle through an array like this:

$myArray = array( '1970-01-01 00:00:01', '1970-01-01 00:01:00' );
$pendingKeys = array();
foreach( $myArray as $key => $myArrayValue ) {
    // NOTE: strtotime( '1970-01-01 00:00:01' ) = 1 (a conflicting key)
    $timestamp = strtotime( $myArrayValue );
    swapKeys( $myArray, $key, $timestamp, $pendingKeys );
}
// RESULT: $myArray == array( 1=>'1970-01-01 00:00:01', 60=>'1970-01-01 00:01:00' )
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
kingjeffrey
  • 14,894
  • 6
  • 42
  • 47
4

this code will help to change the oldkey to new one

$i = 0;
$keys_array=array("0"=>"one","1"=>"two");

$keys = array_keys($keys_array);

for($i=0;$i<count($keys);$i++) {
    $keys_array[$keys_array[$i]]=$keys_array[$i];
    unset($keys_array[$i]);
}
print_r($keys_array);

display like

$keys_array=array("one"=>"one","two"=>"two");
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Karthikeyan Ganesan
  • 1,901
  • 20
  • 23
  • ...but the asker is not seeking a way to apply values as new keys. If that was the task, then PHP already natively offers `array_combine($keys_array, $keys_array)`. Effectively, I'm saying that no one should ever entertain using your snippet -- even if it did offer the desires effect. – mickmackusa Feb 26 '22 at 06:40
4

Easy stuff:

this function will accept the target $hash and $replacements is also a hash containing newkey=>oldkey associations.

This function will preserve original order, but could be problematic for very large (like above 10k records) arrays regarding performance & memory.

function keyRename(array $hash, array $replacements) {
    $new=array();
    foreach($hash as $k=>$v)
    {
        if($ok=array_search($k,$replacements))
            $k=$ok;
        $new[$k]=$v;
    }
    return $new;    
}

this alternative function would do the same, with far better performance & memory usage, at the cost of losing original order (which should not be a problem since it is hashtable!)

function keyRename(array $hash, array $replacements) {

    foreach($hash as $k=>$v)
        if($ok=array_search($k,$replacements))
        {
          $hash[$ok]=$v;
          unset($hash[$k]);
        }

    return $hash;       
}
Nadir
  • 695
  • 8
  • 12
  • Not explicitly checking for `false` when using `array_search()` is problematic. What if the searched key is the first element? I fear you are teaching unreliable practices with this answer. Also, `loosing` is spelled `losing`. – mickmackusa Feb 26 '22 at 05:32
  • Furthermore, assuming that the asker needs to translate all keys in their original array, then you are advising looped calls to a custom function which makes looped function calls without an early `return`/`break`. This is definitely not going to perform well. – mickmackusa Feb 26 '22 at 06:42
  • (fixed losing) - sure if(($ok=array_search($k,$replacements)!==false) will be safer – Nadir Aug 16 '22 at 08:21
3

This page has been peppered with a wide interpretation of what is required because there is no minimal, verifiable example in the question body. Some answers are merely trying to solve the "title" without bothering to understand the question requirements.

The key is actually an ID number and the value is a count. This is fine for most instances, however I want a function that gets the human-readable name of the array and uses that for the key, without changing the value.

PHP keys cannot be changed but they can be replaced -- this is why so many answers are advising the use of array_search() (a relatively poor performer) and unset().

Ultimately, you want to create a new array with names as keys relating to the original count. This is most efficiently done via a lookup array because searching for keys will always outperform searching for values.

Code: (Demo)

$idCounts = [
    3 => 15,
    7 => 12,
    8 => 10,
    9 => 4
];

$idNames = [
    1 => 'Steve',
    2 => 'Georgia',
    3 => 'Elon',
    4 => 'Fiona',
    5 => 'Tim',
    6 => 'Petra',
    7 => 'Quentin',
    8 => 'Raymond',
    9 => 'Barb'
];

$result = [];
foreach ($idCounts as $id => $count) {
    if (isset($idNames[$id])) {
        $result[$idNames[$id]] = $count;
    }
}
var_export($result);

Output:

array (
  'Elon' => 15,
  'Quentin' => 12,
  'Raymond' => 10,
  'Barb' => 4,
)

This technique maintains the original array order (in case the sorting matters), doesn't do any unnecessary iterating, and will be very swift because of isset().

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
2

If you want to replace several keys at once (preserving order):

/**
 * Rename keys of an array
 * @param array $array (asoc)
 * @param array $replacement_keys (indexed)
 * @return array
 */
function rename_keys($array, $replacement_keys)  {
      return array_combine($replacement_keys, array_values($array));
}

Usage:

$myarr = array("a" => 22, "b" => 144, "c" => 43);
$newkeys = array("x","y","z");
print_r(rename_keys($myarr, $newkeys));
//must return: array("x" => 22, "y" => 144, "z" => 43);
lepe
  • 24,677
  • 9
  • 99
  • 108
  • `array_combine()` does not require its parameters to be indexed arrays, so `array_values()` is not necessary. https://3v4l.org/uN1ZF This answer relies in the fact that the second array is already perfectly aligned with the ids in the first array. This is rarely the case in real world projects and not likely to be a reliable solution for researchers. – mickmackusa Feb 26 '22 at 06:14
  • 1
    @mickmackusa I think you are right. I guess the issue here was that the question was not that clear to me. Now re-reading the question (6 years later), I believe that your answer nailed it (anyway... I don't use PHP anymore...hehe). – lepe Feb 26 '22 at 12:00
2

You can use this function based on array_walk:

function mapToIDs($array, $id_field_name = 'id')
{
    $result = [];
    array_walk($array, 
        function(&$value, $key) use (&$result, $id_field_name)
        {
            $result[$value[$id_field_name]] = $value;
        }
    );
    return $result;
}

$arr = [0 => ['id' => 'one', 'fruit' => 'apple'], 1 => ['id' => 'two', 'fruit' => 'banana']];
print_r($arr);
print_r(mapToIDs($arr));

It gives:

Array(
    [0] => Array(
        [id] => one
        [fruit] => apple
    )
    [1] => Array(
        [id] => two
        [fruit] => banana
    )
)

Array(
    [one] => Array(
        [id] => one
        [fruit] => apple
    )
    [two] => Array(
        [id] => two
        [fruit] => banana
    )
)
Alekzander
  • 866
  • 3
  • 12
  • 12
  • This answer (which completely ignores the asker's described scenario) is over-engineering a solution that PHP already offers a native function call for. I would never use your script in any of my codes. I would use this: [`array_column($arr, null, 'id')`](https://3v4l.org/TRT69). But again, I must say that your answer has nothing to do with this asked question which is only processing flat arrays. – mickmackusa Feb 26 '22 at 06:11
2

This basic function handles swapping array keys and keeping the array in the original order...

public function keySwap(array $resource, array $keys)
{
    $newResource = [];

    foreach($resource as $k => $r){
        if(array_key_exists($k,$keys)){
            $newResource[$keys[$k]] = $r;
        }else{
            $newResource[$k] = $r;
        }
    }

    return $newResource;
}

You could then loop through and swap all 'a' keys with 'z' for example...

$inputs = [
  0 => ['a'=>'1','b'=>'2'],
  1 => ['a'=>'3','b'=>'4']
]

$keySwap = ['a'=>'z'];

foreach($inputs as $k=>$i){
    $inputs[$k] = $this->keySwap($i,$keySwap);
}
MikeyJ
  • 454
  • 1
  • 5
  • 16
  • So, are you really advising that the asker should be making looped calls of a custom function that makes full-array loops? ...and no `break`s either??? This will not be an efficient solution. – mickmackusa Feb 26 '22 at 06:29
2

This function will rename an array key, keeping its position, by combining with index searching.

function renameArrKey($arr, $oldKey, $newKey){
    if(!isset($arr[$oldKey])) return $arr; // Failsafe
    $keys = array_keys($arr);
    $keys[array_search($oldKey, $keys)] = $newKey;
    $newArr = array_combine($keys, $arr);
    return $newArr;
}

Usage:

$arr = renameArrKey($arr, 'old_key', 'new_key');
Grant
  • 5,709
  • 2
  • 38
  • 50
1

this works for renaming the first key:

$a = ['catine' => 'cat', 'canine'  => 'dog'];
$tmpa['feline'] = $a['catine'];
unset($a['catine']);
$a = $tmpa + $a;

then, print_r($a) renders a repaired in-order array:

Array
(
    [feline] => cat
    [canine] => dog
)

this works for renaming an arbitrary key:

$a = ['canine'  => 'dog', 'catine' => 'cat', 'porcine' => 'pig']
$af = array_flip($a)
$af['cat'] = 'feline';
$a = array_flip($af)

print_r($a)

Array
(
    [canine] => dog
    [feline] => cat
    [porcine] => pig
)

a generalized function:

function renameKey($oldkey, $newkey, $array) {
    $val = $array[$oldkey];
    $tmp_A = array_flip($array);
    $tmp_A[$val] = $newkey;

    return array_flip($tmp_A);
}
wmmso
  • 55
  • 5
  • Flipping arrays is not a stable solution if the values may contain duplicates. The asked question states that the input data contains ids as keys and counts as values. These counts are likely to contain duplicates. Ergo, flipping is a bad choice. Your earlier written unset&union is hardcoding too much. The asker is not likely to be doing individual/hand-coded element declarations for the entire array. – mickmackusa Feb 26 '22 at 06:25
1

There is an alternative way to change the key of an array element when working with a full array - without changing the order of the array. It's simply to copy the array into a new array.

For instance, I was working with a mixed, multi-dimensional array that contained indexed and associative keys - and I wanted to replace the integer keys with their values, without breaking the order.

I did so by switching key/value for all numeric array entries - here: ['0'=>'foo']. Note that the order is intact.

<?php
$arr = [
    'foo',
    'bar'=>'alfa',
    'baz'=>['a'=>'hello', 'b'=>'world'],
];

foreach($arr as $k=>$v) {
    $kk = is_numeric($k) ? $v : $k;
    $vv = is_numeric($k) ? null : $v;
    $arr2[$kk] = $vv;
}

print_r($arr2);

Output:

Array (
    [foo] => 
    [bar] => alfa
    [baz] => Array (
            [a] => hello
            [b] => world
        )
)
Kristoffer Bohmann
  • 3,986
  • 3
  • 28
  • 35
1

best way is using reference, and not using unset (which make another step to clean memory)

$tab = ['two' => [] ];

solution:

$tab['newname'] = & $tab['two'];

you have one original and one reference with new name.

or if you don't want have two names in one value is good make another tab and foreach on reference

foreach($tab as $key=> & $value) {
    if($key=='two') { 
        $newtab["newname"] = & $tab[$key];
     } else {
        $newtab[$key] = & $tab[$key];
     }
}

Iterration is better on keys than clone all array, and cleaning old array if you have long data like 100 rows +++ etc..

Kamil Dąbrowski
  • 984
  • 11
  • 17
0

One which preservers ordering that's simple to understand:

function rename_array_key(array $array, $old_key, $new_key) {
  if (!array_key_exists($old_key, $array)) {
      return $array;
  }
  $new_array = [];
  foreach ($array as $key => $value) {
    $new_key = $old_key === $key
      ? $new_key
      : $key;
    $new_array[$new_key] = $value;
  }
  return $new_array;
}
Andrew
  • 501
  • 7
  • 15
0

Here is an experiment (test)

Initial array (keys like 0,1,2)

$some_array[] = '6110';//
$some_array[] = '6111';//
$some_array[] = '6210';//

I must change key names to for example human_readable15, human_readable16, human_readable17

Something similar as already posted. During each loop i set necessary key name and remove corresponding key from the initial array.

For example, i inserted into mysql $some_array got lastInsertId and i need to send key-value pair back to jquery.

$first_id_of_inserted = 7;//lastInsertId
$last_loop_for_some_array = count($some_array);


for ($current_loop = 0; $current_loop < $last_loop_for_some_array ; $current_loop++) {

$some_array['human_readable'.($first_id_of_inserted + $current_loop)] = $some_array[$current_loop];//add new key for intial array

unset( $some_array[$current_loop] );//remove already renamed key from array

}

And here is the new array with renamed keys

echo '<pre>', print_r($some_array, true), '</pre>$some_array in '. basename(__FILE__, '.php'). '.php <br/>';

If instead of human_readable15, human_readable16, human_readable17 need something other. Then could create something like this

$arr_with_key_names[] = 'human_readable';
$arr_with_key_names[] = 'something_another';
$arr_with_key_names[] = 'and_something_else';


for ($current_loop = 0; $current_loop < $last_loop_for_some_array ; $current_loop++) {

    $some_array[$arr_with_key_names[$current_loop]] = $some_array[$current_loop];//add new key for intial array

    unset( $some_array[$current_loop] );//remove already renamed key from array

    }
Andris
  • 1,434
  • 1
  • 19
  • 34
  • Please be careful to answer the question in the question body, and not just answer the question title. You will notice from [my answer](https://stackoverflow.com/a/71274431/2943403) that this question does not merely want to `array_combine()` two arrays, but actually rekey an array using a lookup. – mickmackusa Aug 27 '22 at 11:24
  • May be i misunderstood question. Just faced with similar problem and wrote my solution. As i understand question was to keep the same order of array keys, the same values, but need to change names of array keys. Seems the code i posted, solves all this. – Andris Aug 28 '22 at 04:54
-1

Hmm, I'm not test before, but I think this code working

function replace_array_key($data) {
    $mapping = [
        'old_key_1' => 'new_key_1',
        'old_key_2' => 'new_key_2',
    ];

    $data = json_encode($data);
    foreach ($mapping as $needed => $replace) {
        $data = str_replace('"'.$needed.'":', '"'.$replace.'":', $data);
    }

    return json_decode($data, true);
}
Frank Vu
  • 1,203
  • 1
  • 9
  • 19
-1

You can write simple function that applies the callback to the keys of the given array. Similar to array_map

<?php
function array_map_keys(callable $callback, array $array) {
    return array_merge([], ...array_map(
        function ($key, $value) use ($callback) { return [$callback($key) => $value]; },
        array_keys($array),
        $array
    ));
}

$array = ['a' => 1, 'b' => 'test', 'c' => ['x' => 1, 'y' => 2]];
$newArray = array_map_keys(function($key) { return 'new' . ucfirst($key); }, $array);

echo json_encode($array); // {"a":1,"b":"test","c":{"x":1,"y":2}}
echo json_encode($newArray); // {"newA":1,"newB":"test","newC":{"x":1,"y":2}}

Here is a gist https://gist.github.com/vardius/650367e15abfb58bcd72ca47eff096ca#file-array_map_keys-php.

vardius
  • 6,326
  • 8
  • 52
  • 97
  • The asker has no intention of merely prepending a string to the original keys. Please re-read the question requirements. There keys of the input array must be replaced by the associated values from another array. At most, this answer is the correct answer to a different question. At face value, this answer is simply wrong. – mickmackusa Feb 26 '22 at 06:37