PHP foreach循环重复输入

问题描述 投票:1回答:1

我需要帮助才能在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循环以获得正确的输出?

php arrays foreach associative-array
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.