The chain you present is actually considered a JSON. Therefore, there is a proper function to convert a JSON to an array: that function is json_decode
.
But it would be necessary that the variable begin and end with single quotes, and that you pass a second parameter TRUE
to create an array from $variable
, which, as we have said, is not more than a json.
Let's see:
/*Creamos la variable empezando y terminado con '*/
$variable = '[{"id":1, "nombre":"Juan"},{"id":2, "nombre":"Manuel"}]';
/*Pasamos la variable y TRUE a json_decode*/
$arr=json_decode($variable,TRUE);
/*Probamos nuestro array*/
print_r($arr);
Exit:
Array
(
[0] => Array
(
[id] => 1
[nombre] => Juan
)
[1] => Array
(
[id] => 2
[nombre] => Manuel
)
)
Also, we can run it from code to present its values:
foreach ($arr as $row){
echo "id: ".$row["id"]." - nombre: ".$row["nombre"].PHP_EOL;
}
Exit:
id: 1 - nombre: Juan
id: 2 - nombre: Manuel