我收到一个json,如下所示:
[{"name":"banana","country":"Algeria"},{"name":"Apple","country":"China"}]
我如何将其转换为一个数组,其中的关键字是name
?
我使用的是json_decode($json, true);
,但它创建了一个自动增量密钥。
所以我想要这样的东西:
Array
(
[banana] => Array
(
[country] => Algeria
)
[Apple] => Array
(
[country] => China
)
)
谢谢。
你的意思是这样的吗?
json代码:
{
"Banana": {
"country":"Algeria"
},
"Apple": {
"country":"China"
}
}
php代码:
<?php
$fruitsandcountries = json_decode(file_get_contents("filename.json"), true);
echo($fruitsandcountries['Banana']['country']);
?>
输出:
阿尔及利亚