Some easy code, If I have a json data. I want do somethings, first check the match string in the json data, if have, output the value after the match line, else output all the json data.
Exapmle 1, match string is 9, match in the json data, output the value after the match line 7, 3.
$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = '9';
foreach ($array as $data){
echo $data->a;//7, 3
}
Exapmle 2, match string is 2, not match in the json data, output all the value, 5, 9, 7, 3.
$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = '2';
foreach ($array as $data){
echo $data->a;//5, 9, 7, 3
}
How to do this judgment? I do something like in the foreach, just ignore the match string:
if($match_string == $data->a){
continue;//fut this in the foreach ,get 5, 7, 3, but I need 7, 3, next value from 9.
}
Thanks.
You need to set a flag telling you whether or not you’ve found a match: