Test in some json code, like [{"a":"1"},{"a":"2"},{"a":"3"},{"b":"2"}], I want use json decode, get the data and make a judge, if first a nod is == b nod or fisrt a nod is != b nod. some code here.
<?php
header("Content-type: text/html; charset=utf-8");
$json = json_decode('[{"a":"1"},{"a":"2"},{"a":"3"},{"b":"2"}]',true);
$number=1;
foreach($json as $num){
if($num['a']!=$num['b']){
if($num['a']){
echo 'a'.$number.''.$num['a'].'<br />';
}
}else{
if($num['a']){
echo 'b'.$number.''.$num['a'].'<br />';
}
}
$number++;
}
?>
Now my result is:
a11
a22
a33
And I need get the result:
a11
b22
a33
How can you compare non-existing array fields?
there is no $num[‘b’] field, there are these fields, its array[4]
(index 0..3)$numin first iteration will holdso if you wanna compare to
"b":2you have to use this pattern:Array_shift : http://php.net/manual/en/function.array-shift.php