Good morning friends; I want to change the name of the key of an object with php but I don't know how to do it, can you help me?
This is my example object:
$frutas = array(
[
"nombre" => "mango",
"tamaño" => "pequeño"
],
[
"nombre" => "pera",
"tamaño" => "mediano"
]
);
What we want to do is rename the key from "name" to "title" , for each one.
In such a way that if we do a print_r($frutas);
our output is something like this:
Array
(
[0] => Array
(
[title] => mango
[tamaño] => pequeño
)
[1] => Array
(
[title] => pera
[tamaño] => mediano
)
)
The code I had so far was the following:
foreach ($frutas as $key => $value) {
if (key($value) == 'nombre') {
//Aqui ya no se que poner para cambiar el nombre de la clave
}
}
I hope you can help me with my problem and thank you very much for your help.
You can use the PHP array_walk function :
*Recommendation: avoid using special characters like
ñ
when programming (I say it fortamaño
)