[英]how to get a value of multidimensional array on php
hi im bulding a site with php that use a json api and i need to echo the values of an multidimensional array 您好,我正在使用json API与php建立站点,我需要回显多维数组的值
"troopsLevels": [
{
"value": 5,
"globalID": 4000000
},
{
"value": 5,
"globalID": 4000001
},
{
"value": 4,
"globalID": 4000002
},
this is a example of my json file what i need is to show the value "value" knowing depending of the globalID 这是我的json文件的示例,我需要显示的是根据globalID知道的值“ value”
but not sure how to do it 但不确定如何做
i was thinking something like 我在想像
$troop_lvl = $data['troopsLevels'];
if($troop_lvl['globalID'] == 4000000){echo $troop_lvl['value']}
but of curse this will not work as i dont specify the item [0]..[2] 但诅咒这将无法工作,因为我没有指定项目[0] .. [2]
but that actually thats what i need to avoid using [0] to select specific array i need to read all and only show the ['value'] when i give the globalid 但这实际上就是我需要避免使用[0]选择特定数组的原因,我需要读取所有内容并仅在我提供globalid时显示['value']
i really hope yo can understand me english is not my mother language thanks a lot for you help 我真的希望您能理解我英语不是我的母语,非常感谢您的帮助
Use foreach 使用foreach
foreach ($troop_lvl as $key=>$value) {
if($value['globalID'] == 4000000) {
echo $troop_lvl['value'];
}
}
You need to use foreach
loop 您需要使用
foreach
循环
foreach ($troop_lvl as $key=>$value) {
if($value['globalID'] == 4000000) {
echo $value['value'];
}
}
See below: 见下文:
<?php
$arr = array("test" => array("value" => 1, "value2" => 2), "test2" => array("value" => 21, "value2" => 22));
$encode_arr = json_encode($arr);
$decode_arr = json_decode($encode_arr);
//print_r($decode_arr);
foreach ($decode_arr as $key => $value) {
if($value->value2==2)
echo $value->value;
}
?>
The output will be 1
. 输出将为
1
。
This should work for you, 这应该为您工作,
$a = '{"troopsLevels": [
{
"value": 5,
"globalID": 4000000
},
{
"value": 5,
"globalID": 4000001
},
{
"value": 4,
"globalID": 4000002
}
]}';
$abc = json_decode($a);
foreach ($abc->troopsLevels as $row) {
if ($row->globalID == 4000000) {
echo $row->value;// prints value as 5 for the current input.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.