开发者

Wrong values from array

I'm getting wrong values from array.

$json = file_get_contents('http://onleague.stormris开发者_运维技巧e.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] => )


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"}
        ]
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜