0

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
4
  • You want to do what exactly? If first a node is == b node or fisrt a node is != b node, then what? Commented Oct 20, 2011 at 18:38
  • @netcoder, I want make a foreach all the $num['a'] and make out which $num['a'] is equal to $num['b'] such as a11,b22,a33 Commented Oct 20, 2011 at 18:40
  • "Test in some json code", sweet man. Commented Oct 20, 2011 at 18:43
  • hey :) , test for further study. Commented Oct 20, 2011 at 18:47

2 Answers 2

2

How can you compare non-existing array fields?

there is no $num['b'] field, there are these fields, its array[4] (index 0..3)

  • a:1
  • a:2
  • a:3
  • b:2

$num in first iteration will hold

array("a"=>1);

so if you wanna compare to "b":2 you have to use this pattern:

$cmpr = array_shift($json);
...
if($num != $cmpr)

Array_shift : http://php.net/manual/en/function.array-shift.php

Sign up to request clarification or add additional context in comments.

2 Comments

array_pop($json) will output an array. It needs to be array_shift(array_pop($json)) to actually work. Also, this will only work if you have b set in the final value.
yeah, you're right. I've repaired my answer. And yes, this code works only on given data structure
2

You're trying to refer to the b variable when b is not currently defined. you must first loop through and find the b variable. http://codepad.org/0SnrPp6N

<?php
$json = json_decode('[{"a":"1"},{"a":"2"},{"a":"3"},{"b":"2"}]',true);
foreach ($json as $var) {
    if (isset($var['b'])) { $b = $var['b']; break; }
}
$i=0;
foreach ($json as $var) {
    if (!isset($var['a']))
        continue;
    if ($var['a']!=$b) 
        echo "a".++$i."$var[a]\n";
    else
        echo "b".++$i."$var[a]\n";
}

8 Comments

not, if the position of B is always known (last element of array)
@MarekSebera You're correct, but he hasn't specified if it is or not. Because of that, we have to assume it isn't.
@cj333 this isn't complex at all, either of our answers will work correctly. Your problem is as I described, you're referring to the value of key b when key b isn't set.
ok, both of u are right, give the mark to the first anwser and give you two +1, thanks all.
@cj333 if the b key is not always set in the last array, array_pop will be no use. If you are not aware of b's position, use my code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.