How to change the key of array element in PHP?

Posted by dcomartin on Wed, 11 Mar 2020 13:11:57 +0100

I have an associative array in the form of key = > value, where key is a number, but it is not a continuous number. The key is actually an ID number, and the value is a count. This is fine in most cases, but I want a function that takes the readable name of the array and uses it for the key without changing the value.

I don't see the function that does this, but I assume I need to supply the old key and the new key (which I own) and transform the array. Is there an effective way?

#1 building

If your array is recursive, you can use this function: test the following 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,
            )
    );

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

#2 building

The way you do this and keep the array order is to put the array keys in a separate array, find and replace the keys in that array, and then combine them with values.

This is a function to do this:

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

#3 building

You can use a second associative array that maps human readable names to ID S. It will also provide many to one relationships. Then do the following:

echo 'Widgets: ' . $data[$humanreadbleMapping['Widgets']];

#4 building

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

#5 building

If you also want the location of the new array key to be the same as the location of the old array key, you can do the following:

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