2017-05-11 77 views
1

使用PHP和MySQL,我生成了一個名爲$response的數組。PHP Multudimensional Array foreach

A var_dump$response可以看到here

array(2) { 
    ["OperationRequest"]=> 
    array(4) { 
    ["HTTPHeaders"]=> 
    array(1) { 
     [0]=> 
     array(1) { 
     ["@attributes"]=> 
     array(2) { 
      ["Name"]=> 
      string(9) "UserAgent" 
      ["Value"]=> 
      string(14) "ApaiIO [2.1.0]" 
     } 
     } 
    } 
    ["RequestId"]=> 
    string(36) "f53f381e-efb3-4fef-8e39-4f732b4b463e" 
    ["Arguments"]=> 
    array(1) { 
     ["Argument"]=> 
     array(11) { 
     [0]=> 
     array(1) { 
      ["@attributes"]=> 
      array(2) { 
      ["Name"]=> 
      string(14) "AWSAccessKeyId" 
      ["Value"]=> 
      string(20) "KEY" 
      } 
     } 
     [1]=> 
     array(1) { 
      ["@attributes"]=> 
      array(2) { 
      ["Name"]=> 
      string(12) "AssociateTag" 
      ["Value"]=> 
      string(11) "TAG" 
      } 
     } 
     [2]=> 
     array(1) { 
      ["@attributes"]=> 
      array(2) { 
      ["Name"]=> 
      string(6) "IdType" 
      ["Value"]=> 
      string(4) "ISBN" 
      } 
     } 
     [3]=> 
     array(1) { 
      ["@attributes"]=> 
      array(2) { 
      ["Name"]=> 
      string(6) "ItemId" 
      ["Value"]=> 
      string(38) "0751538310,9780141382067,9781305341141" 
      } 
     } 
     [4]=> 
     array(1) { 
      ["@attributes"]=> 
      array(2) { 
      ["Name"]=> 
      string(9) "Operation" 
      ["Value"]=> 
      string(10) "ItemLookup" 
      } 
     }.......so on 

陣列的json_encode可以看出here(如在註釋請求)。

我想從這兩個項目中選擇Title。從我所看到的這個位於;

Items > Item > ItemAttributes > Author 

因此,使用foreach循環,我嘗試了以下;

foreach ($response as $item) { 
    echo $item['Items']['Item']['ItemAttributes']['Title']; // line 2 
} 

但是,這會返回以下錯誤;

消息:未定義的索引:項目。行號:2

我在哪裏出錯了,爲了達到預期的結果,我必須在代碼中更改哪些內容?

此外,任何有關如何「讀」多維數組的建議將不勝感激。

感謝

+0

你需要這樣做: - 'echo $ item ['OperationRequest'] ['Items'] ['Item'] ['ItemAttributes'] ['Title']; ' –

+1

@johnny_s你可以分享上述數組的'json'嗎? –

+0

@johnny_s'echo json_encode($ response);' –

回答

2

試試這個,它會幫助你。你正在迭代錯誤的關鍵,這就是爲什麼你沒有得到期望的輸出。

Try this code snippet herefrom json provide by OP in question

foreach($array["Items"]["Item"] as $key => $value) 
{ 
    print_r($value["ItemAttributes"]["Title"]); 
    echo PHP_EOL; 
} 

輸出:

Panic Panic Captain Flinn and the Pirate Dinosaurs: Missing Treasure! (Captain Flinn)

爲了得到獨一無二的稱號:

foreach(json_decode($json,true)["Items"]["Item"] as $key => $value) 
{ 
    $result[]=$value["ItemAttributes"]["Title"]; 
    echo PHP_EOL; 
} 
print_r(array_unique($result)); 
0

你應該象調試:

foreach ($response as $key => $item) { 
    if(isset($item['Items'])){ //Check Items index defined 
     echo $item['Items']['Item']['ItemAttributes']['Title']; 
    }else{ 
     var_dump($key); 
    } 
} 
+0

這究竟是什麼目的?產生'string(16)「OperationRequest」字符串(5)「Items」'。謝謝 –

+0

因爲你的數組由鍵定義,你必須輸入完全的鍵作爲訪問子數組的路徑。如果數組和鍵不存在,PHP會警告:「未定義索引」 使用「OperationRequest」和「Items」鍵,它沒有子數組$ item ['Items'] =>錯誤 您可以添加此腳本用於調試每個密鑰: var_dump($ key); 死亡; – allready4v

1

@Also,就如何 '讀' 的任何意見多維數組將不勝感激。

安置自己的編碼的JSON字符串

http://json.parser.online.fr

「+」和「 - 」按鈕,在右側面板中應該可以幫助你輕鬆閱讀。

//Check Items is not empty 
if(!isset($response["Items"]["Item"]) || empty($response["Items"]["Item"])) 
    throw New Exception("Empty Item"); 

foreach($response["Items"]["Item"] as $item){ 
    $title = $item['ItemAttributes']['Title'] 
} 
+0

感謝您的解釋和鏈接到http://json.parser.online.fr @Parfait - 都非常有幫助:) –