0
次の配列を並べ替えて、それらの項目タイプに基づいて別のリストにエコーしたいと思います。キー値に基づいてリストに多次元連想配列をエコーする
Array
(
[0] => Array
(
[itemid] => 1
[itemname] => Sword
[itemtype] => Standard
)
[1] => Array
(
[itemid] => 2
[itemname] => Dagger
[itemtype] => Standard
)
[2] => Array
(
[itemid] => 3
[itemname] => Backpack
[itemtype] => Standard
)
[3] => Array
(
[itemid] => 4
[itemname] => Rope (50ft)
[itemtype] => Standard
)
[4] => Array
(
[itemid] => 5
[itemname] => Tinderbox
[itemtype] => Standard
)
[5] => Array
(
[itemid] => 6
[itemname] => Torch
[itemtype] => Standard
)
[6] => Array
(
[itemid] => 6
[itemname] => Torch
[itemtype] => Standard
)
[7] => Array
(
[itemid] => 6
[itemname] => Torch
[itemtype] => Standard
)
[8] => Array
(
[itemid] => 6
[itemname] => Torch
[itemtype] => Standard
)
[9] => Array
(
[itemid] => 6
[itemname] => Torch
[itemtype] => Standard
)
[10] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[11] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[12] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[13] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[14] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[15] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[16] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[17] => Array
(
[itemid] => 8
[itemname] => Bread
[itemtype] => Provisions
)
[18] => Array
(
[itemid] => 9
[itemname] => Healing Salve
[itemtype] => Magical
)
[19] => Array
(
[itemid] => 9
[itemname] => Healing Salve
[itemtype] => Magical
)
)
これをitemtypeキーに基づいてリストにエコーしたいと思います。すなわち:
Standard
=========
Sword Dagger Backpack Rope (50ft) Tinderbox Torch x5
Provisions
=========
Bread x8
Magical
=========
Healing Salve
は私が無駄にこのようにそれをやってみました:
foreach ($array as $value) {
if ($value['itemtype'] == 'Standard'){
echo $value['itemname'] . "\n";
}
foreach ($array as $value) {
if ($value['itemtype'] == 'Provisions'){
echo $value['itemname'] . "\n";
}
foreach ($array as $value) {
if ($value['itemtype'] == 'Magical'){
echo $value['itemname'] . "\n";
}
おそらく最初の別々の配列にこれを分割する方が賢明だろうか?
スイッチをうまく使います。私はこれが本当に良い方法だと思っています。なぜあなたが。=演算子を使用しているのか疑問に思っていますか?それはそれぞれを一つに固めていますか? – Buts
Riiiight私はそれを手に入れます。いい仕事:D – Buts