2012-04-28 99 views
-4

我正在從運行php代碼獲得以下輸出,現在我想從下面的數組中獲得「url」字段,我該如何獲取?如何從數組中獲取特定元素

Array (
    [data] => Array (
     [0] => Array (
      [id] => xxxxx 
      [from] => Array (
       [id] => xxxxxx 
       [name] => abc 
      ) 
      [start_time] => 2012-04-28T07:49:17+0000 
      [end_time] => 2012-04-28T07:49:17+0000 
      [publish_time] => 2012-04-28T07:49:17+0000 
      [application] => Array (
       [id] => 389407107765872 
       [name] => Ki News Social Reader 
      ) 
      [data] => Array (
       [article] => Array (
        [id] => 10150703062718596 
        [url] => http://example.com 
        [type] => kinewssocialreader:article 
        [title] => story 
       ) 
      ) 

在此先感謝!

+3

如何訪問陣列應該是基本常識:http://php.net/manual/en/language.types.array.php – 2012-04-28 16:57:05

+0

見多維數組[這裏](HTTP:// www.w3schools.com/php/php_arrays.asp)。 – Shomz 2012-04-28 16:57:23

回答

3

索引它像任何其他數組。看看print_r的實際來源可能比將它們全部串在一起看起來更有幫助。

$arr['data'][0]['data']['article']['url'] 
0

使用此

$array['data'][0]['data']['article']['url'] 

如果你的數組變量被稱爲$陣列。

0

如果你想與它的鍵來訪問數組:

$array["key_name"] , $array[$key_name] 

如果你想用它的元素,以訪問數組:

$array[0], $array[1] 

如果選擇的結果是數組,您可以繼續使用[]深入:

假設$ array [「key」]包含一個數組,並且該數組具有「another_key」鍵值;

$array["key"]["another_key"] 

也可以工作。

0

假設我正確讀取數據結構,這將是:

$variableName['data'][0]['data']['article']['url'] 

$ VARIABLENAME是頂層數組的名稱。括號不平衡,所以我不能100%確定結構。

我畫出了看起來像這樣的結構。如果這符合你的結構,上面的命名將起作用。

Array ( 
    [data] => Array ( 
    [0] => Array ( 
     [id] => xxxxx 
     [from] => Array ( 
     [id] => xxxxxx 
     [name] => abc 
    ) 
     [start_time] => 2012-04-28T07:49:17+0000 
     [end_time] => 2012-04-28T07:49:17+0000 
     [publish_time] => 2012-04-28T07:49:17+0000 
     [application] => Array ( 
     [id] => 389407107765872 
     [name] => Ki News Social Reader 
    ) 
     [data] => Array ( 
     [article] => Array ( 
      [id] => 10150703062718596 
      [url] => http://example.com 
      [type] => kinewssocialreader:article [title] => story 
     ) 
    ) 
    ) 
) 
) 
1
$url=$myArray['data'][0]['data']['article']['url']; //assuming $myArray is array 
相關問題