Php 如何从Json stdclass数组中提取特定行?

Php 如何从Json stdclass数组中提取特定行?,php,json,Php,Json,这是一个Api调用 $json_string = '"offices": [{"description": "Google Headquarters", "address1": "1600 Amphitheatre Parkway", "address2": "", "zip_code": "", "city": "Mountain View", "state_code": "CA",

这是一个Api调用

   $json_string = '"offices":
      [{"description": "Google Headquarters",
        "address1": "1600 Amphitheatre Parkway",
        "address2": "",
        "zip_code": "",
        "city": "Mountain View",
        "state_code": "CA",
        "country_code": "USA",
        "latitude": 37.421972,
        "longitude": -122.084143},
       {"description": "Google Ann Arbor",
        "address1": "112 S. Main St.",
        "address2": "2nd Floor",
        "zip_code": "48104",
        "city": "Ann Arbor",
        "state_code": "MI",
        "country_code": "USA",
        "latitude": 42.280988,
        "longitude": -83.748882},
       {"description": "Google Atlanta",
        "address1": "10 10th Street NE",
        "address2": "Suite 600",
        "zip_code": "30309"';//more but trimming here.

$obj=json_decode($json_string);
foreach($obj->offices as $office) {
    echo $office->address1;

     }  

这给出了address1的全部结果。但我只需要第一个地址的内容1。我该怎么做呢?

$obj->offices[0]->address1
应该可以工作。

$obj->offices[0]->address1
应该可以工作。

注意到没有人解释过为什么,你在一个对象列表中循环,每个对象都有address1属性,您需要按照galambalazs的回答松开循环并仅引用第一个成员注意到nobodies解释了为什么,您正在循环一个对象列表,每个对象都有address1属性,您需要松开循环并仅按照galambalazs的回答引用第一个成员
$offices  = $obj->offices;    // offices is a key of $obj
$first    = $offices[0];      // its value is an array, we want index 0
$address1 = $first->address1; // this is an object, we want it's address1 key