1

I'm getting wrong values from array.

$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Utils/Countries');
$data = json_decode($json, TRUE);
$countries = array(); 
foreach($data['data'] as $item) {
    $countries[] = $item['description'];
}
print_r($countries);

the results are:

Array ( [0] => g [1] => )
0

2 Answers 2

5

You're not traversing the object correctly.

foreach($data['data']['item'] as $item) {
    $countries[] = $item['description'];
}

It might help if you view the data with some white space.

{
    "valid": true,
    "id": "0",
    "data": {
        "@type": "genericObjectArray",
        "item": [
            {"id": "DE", "description": "Deutschland"},
            {"id": "ES", "description": "España"},
            {"id": "FR", "description": "France"},
            {"id": "PT", "description": "Portugal"},
            {"id": "UK", "description": "United Kingdom"},
            {"id": "US", "description": "United States"}
        ]
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

how can I add "id" values to my array?
Yours suggestion '$countries[] = $item;' gives me this: Array Array Array Array Array
1

I had a problem so crazy than print an array by print_r getting a value on a position and getting that value by index getting another (a value that print_r show in another position) it helped me a lot: How to convert an array to object in PHP?.
I created a stdClass like another answer on same question of link above and iterated puting values on

 $obj->"X".index = value 

getting something like

 $obj->p1,$obj->p2,... 

with values, after used it to fill my real obj like

 $realobj1->myprop1 = $obj2->p1...   

Its nasty but solved my problem

Comments

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.