2017-12-22 17 views
1

PHPの間に多次元配列を操作するには助けが必要ですforeachループ。 I以下の配列している:PHP foreachループ繰り返しエントリ

$sports = [ 
    [ 
     "id" => "soccer", 
     "name" => "Football", 
     "categories" => [ 
      [ 
       "id" => "s1", 
       "name" => "category 1", 
       "sportID" => "soccer" 
      ], 
      [ 
       "id" => "s2", 
       "name" => "category 2", 
       "sportID" => "soccer" 
      ], 
     ], 
     "img" => "/test.png" 
    ], 
    [ 
     "id" => "tennis", 
     "name" => "Tennis", 
     "categories" => [ 
      [ 
       "id" => "t1", 
       "name" => "category 1", 
       "sportID" => "tennis" 
      ], 
      [ "id" => "t2", 
       "name" => "category 2", 
       "sportID" => "tennis" 
      ], 
     ], 
     "img" => "/test.png" 
    ], 
]; 

は、私は、すべてのスポーツのための対応するカテゴリとすべてのスポーツを取るためにforeachの下を持って

foreach($sports as $s){ 
    $categories = $s['categories']; 

    foreach($categories as $c){ 
     $cats[] = [ 
      "id" => $c['id'], 
      "name" => $c['name'], 
      "sportID" => $s['id'], 
     ]; 
    } 

    $allCategories[] = $cats; 

    $data[] = [ 
     "id" => $s['id'], 
     "name" => $s['name'], 
     "categories" => $cats, 
     "img" => $s['img'], 
    ]; 

    $output[] = $data; 
} 

が、出力は私が期待したものではありません。あなたはテニスのカテゴリにあなたはまた、サッカーのカテゴリを見ることができるように、これは、正しく出力されません想像できるように

array(2) { 
    [0]=> 
    array(1) { 
    [0]=> 
    array(4) { 
     ["id"]=> 
     string(8) "soccer" 
     ["name"]=> 
     string(8) "Football" 
     ["categories"]=> 
     array(2) { 
     [0]=> 
     array(3) { 
      ["id"]=> 
      string(2) "s1" 
      ["name"]=> 
      string(10) "category 1" 
      ["sportID"]=> 
      string(8) "soccer" 
     } 
     [1]=> 
     array(3) { 
      ["id"]=> 
      string(2) "s2" 
      ["name"]=> 
      string(10) "category 2" 
      ["sportID"]=> 
      string(8) "soccer" 
     } 
     } 
     ["img"]=> 
     string(9) "/test.png" 
    } 
    } 
    [1]=> 
    array(2) { 
    [0]=> 
    array(4) { 
     ["id"]=> 
     string(8) "soccer" 
     ["name"]=> 
     string(8) "Football" 
     ["categories"]=> 
     array(2) { 
     [0]=> 
     array(3) { 
      ["id"]=> 
      string(2) "s1" 
      ["name"]=> 
      string(10) "category 1" 
      ["sportID"]=> 
      string(8) "soccer" 
     } 
     [1]=> 
     array(3) { 
      ["id"]=> 
      string(2) "s2" 
      ["name"]=> 
      string(10) "category 2" 
      ["sportID"]=> 
      string(8) "soccer" 
     } 
     } 
     ["img"]=> 
     string(9) "/test.png" 
    } 
    [1]=> 
    array(4) { 
     ["id"]=> 
     string(10) "tennis" 
     ["name"]=> 
     string(8) "Tennis" 
     ["categories"]=> 
     array(4) { 
     [0]=> 
     array(3) { 
      ["id"]=> 
      string(2) "s-1" 
      ["name"]=> 
      string(10) "category 1" 
      ["sportID"]=> 
      string(8) "soccer" 
     } 
     [1]=> 
     array(3) { 
      ["id"]=> 
      string(2) "s2" 
      ["name"]=> 
      string(10) "category 2" 
      ["sportID"]=> 
      string(8) "soccer" 
     } 
     [2]=> 
     array(3) { 
      ["id"]=> 
      string(2) "t1" 
      ["name"]=> 
      string(10) "category 1" 
      ["sportID"]=> 
      string(10) "tennis" 
     } 
     [3]=> 
     array(3) { 
      ["id"]=> 
      string(2) "t2" 
      ["name"]=> 
      string(10) "category 2" 
      ["sportID"]=> 
      string(10) "tennis" 
     } 
     } 
     ["img"]=> 
     string(9) "/test.png" 
    } 
    } 
} 

:代わりに、私は以下のような繰り返しの結果を取得しています。 正しい出力を得るために、私のforeachループを訂正するにはどうしたらいいですか?

+0

期待される出力は? – kunal

+0

あなたは各スポーツループで '$ cats'をリセットしていません。 –

+0

私は例の配列と同じ出力を持つ必要があります。これはスポーツの配列であり、この特定のスポーツのカテゴリの中だけです。 – Dion

答えて

5

$cats$dataアレイをリセットすることを忘れてしまいます。

<?php 

$sports = [ 
    [ 
    "id" => "soccer", 
    "name" => "Football", 
    "categories" => [ 
     [ 
      "id" => "s1", 
      "name" => "category 1", 
      "sportID" => "soccer" 
     ], 
     [ 
      "id" => "s2", 
      "name" => "category 2", 
      "sportID" => "soccer" 
     ], 
    ], 


    "img" => "/test.png" 
    ], 
    [ 
    "id" => "tennis", 
    "name" => "Tennis", 
    "categories" => [ 
     [ 
      "id" => "t1", 
      "name" => "category 1", 
      "sportID" => "tennis" 
     ], 
     [ "id" => "t2", 
      "name" => "category 2", 
      "sportID" => "tennis" 
     ], 

    ], 

    "img" => "/test.png" 
    ], 

    ]; 

foreach($sports as $s){ 
    $categories = $s['categories']; 

    # before inner loop we need to reset both arrays 
    $cats = []; 
    $data = []; 

    foreach($categories as $c){ 
     $cats[] = [ 
       "id" => $c['id'], 
       "name" => $c['name'], 
       "sportID" => $s['id'], 
      ]; 
    } 

    $allCategories[] = $cats; 


    $data[] = [ 
     "id" => $s['id'], 
     "name" => $s['name'], 
     "categories" => $cats, 
     "img" => $s['img'], 
     ]; 

     $output[] = $data; 

} 

echo '<PRE>'; 
print_r($output); 
+1

それは完璧に動作します:)ありがとう。 – Dion

関連する問題