array(7) {" />
开发者

Getting a value from an array

I have an PHP array parsed from a JSON string that looks like this:

array(1) {
  ["ResultSet"]=>
  array(7) {
    ["version"]=>
    string(3) "1.0"
    ["Error"]=>
    int(0)
    ["ErrorMessage"]=>
    string(8) "No error"
    ["Locale"]=>
    string(5) "us_US"
    ["Quality"]=>
    int(99)
    ["Found"]=>
    int(1)
    ["Results"]=>
    array(1) {
      [0]=>
      array(29) {
        ["quality"]=>
        int(72)
        ["latitude"]=>
        string(9) "34.746479"
        ["longitude"]=>
        string(10) "-92.289589"
        ["offsetlat"]=>
        string(9) "34.746479"
        ["offsetlon"]=>
        string(10) "-92.289589"
        ["radius"]=>
        int(500)
        ["name"]=>
        string(20) "34.746479,-92.289589"
        ["line1"]=>
        string(13) "State Capitol"
        ["line2"]=>
        string(22) "Little Rock, AR  72201"
        ["line3"]=>
        string(0) ""
        ["line4"]=>
        string(13) "United States"
        ["house"]=开发者_C百科>
        string(0) ""
        ["street"]=>
        string(13) "State Capitol"
        ["xstreet"]=>
        string(0) ""
        ["unittype"]=>
        string(0) ""
        ["unit"]=>
        string(0) ""
        ["postal"]=>
        string(5) "72201"
        ["neighborhood"]=>
        string(0) ""
        ["city"]=>
        string(11) "Little Rock"
        ["county"]=>
        string(14) "Pulaski County"
        ["state"]=>
        string(8) "Arkansas"
        ["country"]=>
        string(13) "United States"
        ["countrycode"]=>
        string(2) "US"
        ["statecode"]=>
        string(2) "AR"
        ["countycode"]=>
        string(0) ""
        ["hash"]=>
        string(0) ""
        ["woeid"]=>
        int(12789127)
        ["woetype"]=>
        int(11)
        ["uzip"]=>
        string(5) "72201"
      }
    }
  }
}

And I am trying to get the value of the woeid parameter like this:

 foreach ($data["ResultSet"] as $key => $val) 
 {
    echo $val["woeid"]."<br />";
 }  

But for some reason it isn't getting the value. Am I doing something wrong?

Thanks!


You need $data['ResultSet']['Results'], not $data['ResultSet']


The first $key => $val will be version => 1.0. The strong 1.0 isn't an array and does't have a woeid.

I'm not going to try to decode your single line expression of a huge data structure, but you need to navigate to the point where the data you want actually appears.


$data["ResultSet"] is an object, the actual results are in $data["ResultSet"]["Results"]. So use:

 foreach ($data["ResultSet"]["Results"] as $key => $val) 
 {
    echo $val["woeid"]."<br />";
 }  
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜