Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello guys just need a little help here. Because I have a json data and I decode it. But I can't access it using the foreach loop. When I tried to print the array structure I got this:

Array
(
    [0] => stdClass Object
        (
            [code] => AD
            [country] => Andorra
        )

    [1] => stdClass Object
        (
            [code] => AE
            [country] => United Arab Emirates
        )

    [2] => stdClass Object
        (
            [code] => AF
            [country] => Afghanistan
        )

    [3] => stdClass Object
        (
            [code] => AG
            [country] => Antigua and Barbuda
        )

    .
    .
    .

All I want is to access the code and country

I used this loop but it display the index name and the values:

foreach($decode_country as $p){
   foreach($p as $key => $value){
      echo $key."--".$value."<br />";
   }
}

But it display:

code--AD
country--Andorra
code--AE
country--United Arab Emirates
code--AF
country--Afghanistan
code--AG
country--Antigua and Barbuda
code--AI
country--Anguilla
code--AL
country--Albania
code--AM
country--Armenia
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Try like

foreach($decode_country as $p){
   echo "code -- ".$p->code."<br>";
   echo "country -- ".$p->country."<br>";
}

Here $p will be considered as an object and you can extract the code and country using $p->code and $p->country.Or Better : while decoding json data you need to give like

$decode_country = json_decode($data,true);

true will return the array result.Then use

foreach($decode_country as $p){
   echo "code -- ".$p['code']."<br>";
   echo "country -- ".$p['country']."<br>";
}
share|improve this answer
    
I also included the boolean TRUE in my decoding but same effect. I got Trying to get property of non-object –  Jerielle Nov 26 '13 at 8:39

You may try this

foreach($decode_country as $p){
    echo $p->code;
    echo $p->country;
}

Here $decode_country is an array of objects and inside foreach loop, each $p is an object.

If you use json_decode($data, true); then use

echo $p['code'];

When TRUE is used, returned objects will be converted into associative arrays. Otherwise, use

echo $p->code;
share|improve this answer
    
Ok I got an error: Trying to get property of non-object –  Jerielle Nov 26 '13 at 8:34
    
That's weired, var_dump($decode_country[0]) should print first object in the array, try this. –  The Alpha Nov 26 '13 at 8:36
    
When I var_dump it. It results to: array(2) { ["code"]=> string(2) "AD" ["country"]=> string(7) "Andorra" } –  Jerielle Nov 26 '13 at 8:37
    
Can you show more code with json_decode ? Use json_decode($data); because you are using object notation. –  The Alpha Nov 26 '13 at 8:41
1  
Ok I got an answer from Gautam3164. Thanks for the suggestions. :) –  Jerielle Nov 26 '13 at 8:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.