2016-09-04 2 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

관련 문제