2024-04-28 10:01:17
$json_str = '[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]';
$arr = json_decode($json_str , true);
foreach($arr as $v){
if($v['a'] == 1){
echo $v['c'];
}
}
2024-04-28 00:55:38
<?php
$json_str = '[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]';
$arr = json_decode($json_str , true);
/**
** $json_str json字符串
** $limit 限制条件数组 如:$limit=array('a','==1');
** $key 返回的键值
**/
function getValueByKey($json_str,$limit=array(),$key){
$arr=json_decode($json_str,true);
foreach($arr as $v){
if($v[$limit[0]].$limit[1]){
return $v[$key];
}
}
}
$data=getValueByKey($json_str,array('a','==1'),'c');
?>
2024-04-28 04:48:15
感谢你的回答,不过成员个数不定而且a,c之间不一定有b,好像还是for稳定兼容好些