0

私が持っている配列は次のようになります。

$items = array();
$items["GB225"] = array (
    "name" => "AAA",
    "img" => "aaa.jpg",
    "includes" => array(
         $things[08] = array (
              "name" => "xxx",
              "text" => "xxxx xx xxxx x x  x xxx x x"
         );
         $things[77] = array (
              "name" => "yyy",
              "text" => "yyyyyy yy yyyyyy y yy yyyyy"
         ) ;
         $things[42] = array (
              "name" => "zzz",
              "text" =>"zz zzzz zzz z z zzz z"
         );
    );
);

私が取得する必要があるのは、できれば PHP を使用して、2 番目の配列要素のそれぞれの ID と名前です (ID = 08 の xxx、ID = 77 の yyy、ID = 42 の zzz が必要です)。

これまでの私の最良の推測は

foreach ($items["includes"] as $thing_id => $thing) { 
     echo $thing["name"];
     echo $thing_id;
}; 

しかし、これは「名前」に関連付けられたIDの0、1、および2のみを提供します。

これを正しく行うにはどうすればよいですか?

4

2 に答える 2

1

あなたのスクリプトの $things 変数は何ですか? この変数は初期化されていないようです。

コードは次のようになります

<?php
$items["GB225"] = array (
    "name" => "AAA",
    "img" => "aaa.jpg",
    "includes" => array(
        8 => array (
              "name" => "xxx",
              "text" => "xxxx xx xxxx x x  x xxx x x"
         ),
         77 => array (
              "name" => "yyy",
              "text" => "yyyyyy yy yyyyyy y yy yyyyy"
         ),
         42 => array (
             "name" => "zzz",
              "text" =>"zz zzzz zzz z z zzz z"
         )
    )
);

foreach ($items['GB225']["includes"] as $thing_id => $thing) { 
    echo $thing["name"];
     echo $thing_id;
}

デモはこちらhttps://eval.in/55194

于 2013-10-17T19:27:16.780 に答える
0

コードは次のようになります。

<?php
$items = array();
$items["GB225"] = array (
    "name" => "AAA",
    "img" => "aaa.jpg",
    "includes" => array(
         8 => array (
              "name" => "xxx",
              "text" => "xxxx xx xxxx x x  x xxx x x"
         ),
         77 => array (
              "name" => "yyy",
              "text" => "yyyyyy yy yyyyyy y yy yyyyy"
         ),
         42 => array (
              "name" => "zzz",
              "text" =>"zz zzzz zzz z z zzz z"
         )
    )
);
echo "<pre>";
print_r($items);

方法 1:

foreach ($items as $key => $val) {
  foreach ($val as $key => $anArr) {
    if ($key == "includes")  {
      foreach ($anArr as $key => $val) {
        echo $key . " : " . $anArr[$key]['name'];
      }
    }
  }
}

方法 2:

foreach ($items['GB225']["includes"] as $thing_id => $thing) { 
  echo $thing["name"];
  echo $thing_id;
}
于 2013-10-17T19:36:15.270 に答える