简体   繁体   中英

Access an array value from a JSON response using PHP

I am new to JSON and I am having a problem accessing particular values in the JSON array that is being returned from the API call. For instance, I would like to access the name value in the response data below (these are the first two responses from the API call). I know I am missing something obvious but I can't quit get it. Any help would be appreciated:

ReadResponse::__set_state(array(
   0 => 
  array (
    'address' => '6866 N Rochester Rd',
    'category' => 'Food & Beverage > Restaurants',
    'category_ids' => 
    array (
      0 => 347,
    ),
    'category_labels' => 
    array (
      0 => 
      array (
        0 => 'Social',
        1 => 'Food and Dining',
        2 => 'Restaurants',
      ),
    ),
    'country' => 'us',
    'factual_id' => '8e6aacfa-b435-407d-83f9-90cec16c1cf8',
    'latitude' => 42.69804075,
    'locality' => 'Rochester Hills',
    'longitude' => -83.134586375,
    'name' => 'Sushi Little Tokyo',
    'neighborhood' => 
    array (
      0 => '\\Livernois-Tienken\\',
    ),
    'postcode' => '48306',
    'region' => 'MI',
    'status' => '1',
    'tel' => '(248) 608-1260',
  ),
   1 => 
  array (
    'address' => '2964 S Rochester Rd',
    'category' => 'Food & Beverage > Restaurants',
    'category_ids' => 
    array (
      0 => 347,
    ),
    'category_labels' => 
    array (
      0 => 
      array (
        0 => 'Social',
        1 => 'Food and Dining',
        2 => 'Restaurants',
      ),
    ),
    'country' => 'us',
    'factual_id' => '35d2fc06-a941-4dbf-9dcb-204964fec730',
    'latitude' => 42.636786765426,
    'locality' => 'Rochester Hills',
    'longitude' => -83.131888674506,
    'name' => 'Pudthi and Sushi',
    'postcode' => '48307',
    'region' => 'MI',
    'status' => '1',
    'tel' => '(248) 299-6890',
    'website' => 'http://www.pudthaiandsushi.com',
  ),

This is the result of a var_export. The data is all contained in an instance of the ReadResponse . To find out how you can access the data, read the source or documentation of that class.

尝试使用此PHP函数json_decode()将JSON响应转换为PHP多维数组,您可以在其中轻松访问数组中的值,例如名称为

$name = $myPHPArray->country->name; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM