2016-09-04 20 views
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"; 
     } 

おそらく最初の別々の配列にこれを分割する方が賢明だろうか?

答えて

1

また、3種類の文字列にアイテムを追加し、最後に連結した文字列をエコーできます

$standard = ''; 
$provisions = ''; 
$magical = ''; 

foreach ($array as $value) { 
    switch $value['itemtype']{ 
     case 'Standard': 
      $standard .= $value['itemname'] . "\n"; 
      break; 
     case 'Provisions': 
      $provisions .= $value['itemname'] . "\n"; 
      break; 
     case 'Magical': 
      $magical .= $value['itemname'] . "\n"; 
      break; 
     default: 
      break; 
    } 
} 
echo $standard . $provisions . $magical; 

だから、あなただけのアレイの上に一回ループする必要があるだろうし、あなたが簡単に多くの例を追加することができます。

+0

スイッチをうまく使います。私はこれが本当に良い方法だと思っています。なぜあなたが。=演算子を使用しているのか疑問に思っていますか?それはそれぞれを一つに固めていますか? – Buts

+0

Riiiight私はそれを手に入れます。いい仕事:D – Buts