簡體   English   中英

使用PHP從JSON響應訪問數組值

[英]Access an array value from a JSON response using PHP

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

這是var_export的結果。 數據全部包含在ReadResponse的實例中。 要了解如何訪問數據,請閱讀該類的源代碼或文檔。

嘗試使用此PHP函數json_decode()將JSON響應轉換為PHP多維數組,您可以在其中輕松訪問數組中的值,例如名稱為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM