【发布时间】:2021-02-17 12:11:51
【问题描述】:
<?php
$str = '[
{
"node":{
"id": "bitcoin",
"name": "Bitcoin",
"price_usd": "610.471"
}
},
{
"node":{
"id": "ethereum",
"name": "Ethereum",
"price_usd": "12.0771"
}
}
]';
$result = json_decode($str, true);
$key = array_search('bitcoin', array_column($result,'node','id'));
echo $result[$key]['price_usd']; // i need 610.471 here
?>
我有一个像上面这样的长 json 代码,我需要通过搜索“id”名称来获取“price_usd”值。 我不想要 $str[0]["node"]["price_usd"]
【问题讨论】:
-
与 JSON 解析无关,因为在您搜索时,您不再使用 JSON (
$str)。