搜索JSON PHP
我有这样的JSON(为了可读性而缩短): 我有一个PHP变量,它等于三个ID中的一个。我需要在JSON中搜索搜索JSON PHP,php,json,search,Php,Json,Search,我有这样的JSON(为了可读性而缩短): 我有一个PHP变量,它等于三个ID中的一个。我需要在JSON中搜索id,并返回本地化名称。这就是我目前正在尝试的 $heroid = $myplayer['hero_id']; $heroes = file_get_contents("data/heroes.json"); $heroesarray = json_decode($heroes, true); foreach ($heroesarray as $parsed_key => $pa
id
,并返回本地化名称。这就是我目前正在尝试的
$heroid = $myplayer['hero_id'];
$heroes = file_get_contents("data/heroes.json");
$heroesarray = json_decode($heroes, true);
foreach ($heroesarray as $parsed_key => $parsed_value) {
if ($parsed_value['id'] == $heroid) {
$heroname = $parsed_value['localized_name'];
}
}
获取JSON字符串并(如果第二个参数为true
)将其转换为关联数组。然后,我们使用循环遍历这些数据,直到找到您想要的英雄
$json = ''; // JSON string
$data = json_decode($json, true);
foreach($data['heroes'] as $hero) {
if($hero['id'] === 2) {
var_dump($hero['localized_name']);
// Axe
// This will stop the loop, if you want to keep going remove this line
break;
}
}
简单。就用吧。解释遵循底部的代码
// First set the ID you want to look for.
$the_id_you_want = 2;
// Next set the $json.
$json = <<<EOT
{
"heroes": [
{
"name": "antimage",
"id": 1,
"localized_name": "Anti-Mage"
},
{
"name": "axe",
"id": 2,
"localized_name": "Axe"
},
{
"name": "bane",
"id": 3,
"localized_name": "Bane"
}
]
}
EOT;
// Now decode the json & return it as an array with the `true` parameter.
$decoded = json_decode($json, true);
// Set to 'TRUE' for testing & seeing what is actually being decoded.
if (FALSE) {
echo '<pre>';
print_r($decoded);
echo '</pre>';
}
// Now roll through the decoded json via a foreach loop.
foreach ($decoded as $decoded_array_key => $decoded_array_value) {
// Since this json is an array in another array, we need anothe foreach loop.
foreach ($decoded_array_value as $decoded_key => $decoded_value) {
// Do a comparison between the `$decoded_value['id']` and $the_id_you_want
if ($decoded_value['id'] == $the_id_you_want) {
echo $decoded_value['localized_name'];
}
}
}
首先你有一个数组,它可以等同于$decoded[0]
,然后在下面你有另一个数组,它等同于$decoded[0]['heros']
,然后在其中有一个数组,它包含的值的结构为$decoded[0]['heros'][0]
,$decoded[0]['heros'][1]
,$decoded[0][“英雄”][2]
但解决这个问题的关键是
print\u r($decoded)
帮助我查看JSON的更大结构。使用foreach
循环并遍历解码的数组。到目前为止,您尝试了什么?使用json_decode函数将json转换为PHP数组。查看示例代码,您遇到了与我相同的错误。您还有1个级别的数组数据要滚动。看看我的答案。在上面的示例中,尝试将foreach($heroesarray
更改为foreach($heroesarray[0]
)。我的答案通过另一个循环来实现相同的目标。这对我不起作用,我将用我正在尝试的内容编辑上面的帖子。@ChrisByatt好的。现在尝试一下。它应该可以正常工作。将编辑并给出解释。
// First set the ID you want to look for.
$the_id_you_want = 2;
// Next set the $json.
$json = <<<EOT
{
"heroes": [
{
"name": "antimage",
"id": 1,
"localized_name": "Anti-Mage"
},
{
"name": "axe",
"id": 2,
"localized_name": "Axe"
},
{
"name": "bane",
"id": 3,
"localized_name": "Bane"
}
]
}
EOT;
// Now decode the json & return it as an array with the `true` parameter.
$decoded = json_decode($json, true);
// Set to 'TRUE' for testing & seeing what is actually being decoded.
if (FALSE) {
echo '<pre>';
print_r($decoded);
echo '</pre>';
}
// Now roll through the decoded json via a foreach loop.
foreach ($decoded as $decoded_array_key => $decoded_array_value) {
// Since this json is an array in another array, we need anothe foreach loop.
foreach ($decoded_array_value as $decoded_key => $decoded_value) {
// Do a comparison between the `$decoded_value['id']` and $the_id_you_want
if ($decoded_value['id'] == $the_id_you_want) {
echo $decoded_value['localized_name'];
}
}
}
Array
(
[heroes] => Array
(
[0] => Array
(
[name] => antimage
[id] => 1
[localized_name] => Anti-Mage
)
[1] => Array
(
[name] => axe
[id] => 2
[localized_name] => Axe
)
[2] => Array
(
[name] => bane
[id] => 3
[localized_name] => Bane
)
)
)