使用PHP从JSON响应访问数组值

时间:2013-04-12 19:18:24

标签: php arrays json api

我是JSON的新手,我在访问从API调用返回的JSON数组中的特定值时遇到问题。例如,我想访问下面的响应数据中的名称值(这些是API调用的前两个响应)。我知道我错过了一些明显但我不能放弃的东西。任何帮助将不胜感激:

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',
  ),

2 个答案:

答案 0 :(得分:0)

这是var_export的结果。数据全部包含在ReadResponse的实例中。要了解如何访问数据,请阅读该类的源代码或文档。

答案 1 :(得分:0)

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

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