How to change a PHP array key

1

I need to change the key of an array

$task=array('Title' => 'Test', 'Description' => 'Description test')

I need to change the name of the key Description

Is there any way?

I'm working with the Laravel framework

    
asked by anonymous 01.10.2018 / 20:32

3 answers

3

It sounds simple, but it's not that trivial.

The answer to your question is: Not possible . Having said that you would ask me "how is it not possible if the other answers showed how to do it?" and I would respond "they lied (at least omitted) to you".

PHP, by internal decision, has decided to define a "generic" structure to work with almost all other data structures, so much so that it is not difficult for you to read that PHP is oriented to array - almost everything is array in PHP. As well as documentation brings:

  

An array in PHP is actually an ordered map. A map is a type that relates values to keys. This type is optimized for a number of different uses: it can be treated as a array , a list (vector), hashtable , stack, queue, and probably more.

It may have gone unnoticed, but the definition begins with the most important part:

  

An array in PHP is actually a map [...] that relates values to keys.

And you can not change the key of a map. What you can do is copy the value of a key, creating a new one and deleting the old key, but that is not changing. This is what the answer from Lucas Azambuja did and is the most practical way to get the desired result. You could still abstract this for a function:

function array_replace_key(&$arr, $old, $new, $overwrite = true): bool {
    if (isset($arr[$new]) and !$overwrite) {
        return false;
    }

    $arr[$new] = $arr[$old];
    unset($arr[$old]);

    return true;
}

See function at Repl.it | Ideone

To "replace" the key description with descrição , for example, it would suffice to do array_replace_key($task, 'description', 'descrição') . If the descrição key already exists in the array it will be overwritten; if this is not the desired behavior, you can pass the last argument as false that the existing value will be kept unchanged, array_replace_key($task, 'description', 'descrição', false) . The function will return a boolean indicating whether or not the key was replaced.

It is worth remembering that since the value is copied from one key to another, depending on what the value is, you may have problems with memory, because during the execution of the function you will have two objects equal.

The solution with array_merge tends to be bad in most cases, as it keeps 3 copies of its object in memory (one in the original object, one in the function parameter and the third in the new array to be merged), it will do an operation that is directly dependent on the size of the array (at least O (N)). This means that if you have an array with thousands of values, the operation will take longer, even if you only want to change a key.

As for the solution using functions defined by Laravel I do not know how to evaluate. I do not know if the functions only do what they promise and in what way they do, but initially I find it unnecessary to use functions of a framework for such. If there are any advantages to using them maybe Marcos Xavier will know how to respond better.

    
02.10.2018 / 15:51
4
<?php 

$task=array('Title' => 'Test', 'Description' => 'Description test');
$task['NovoNome'] = $task['Description'];
unset($task['Description']);

?>
    
01.10.2018 / 21:52
0

In the Laravel way of doing. I used the contracted syntax of array $ array = ['...']; (available from PHP 5.4 link )

$task = ['Title' => 'Test', 'Description' => 'Description test'];

//remove a chave Description,equivalente a unset($task['Description'])
array_forget($task, 'Description');
dump($task);

$task = array_add($task,'New Description','New description test');
//add no início do array
$task = array_prepend($task,'First Key','First');
dump($task);

Up. Tested on version 5.4 and 5.5 of Laravel

    
01.10.2018 / 22:33