2017-05-11 10 views
1

PHPとMySQLを使用して、$responseという配列を生成しました。PHPマルチディメンション配列foreach

は、hereで見ることができる。 (コメントで要求されるように)アレイのjson_encode

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 

hereを見ることができます。

この2つの項目から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 "string(5)" Items "を生成します。ありがとう –

+0

あなたの配列はキーで定義されているので、アクセス子配列のパスとして正確にキーを入力する必要があります。 "未定義のインデックス" "OperationRequest"と "Items"キーで、子配列$ item ['Items'] =>エラー このスクリプトを追加することができます各キーをデバッグします。 var_dump($ key); ダイ; – allready4v

1

@Alsoは、多次元配列を '読み取り' にする方法上の任意のアドバイスをいただければ幸いです。

http://json.parser.online.fr

ポストあなたのエンコードされたJSON文字列は、「+」と「 - 」右側のパネルにあるボタンを使用すると、簡単にそれを読んで役立つはずです。

//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 @パルフェットへのリンクありがとう - 両方の非常に有用:) –