【问题标题】:PHP foreach loop repeated entriesPHP foreach 循环重复条目
【发布时间】:2017-12-22 10:24:44
【问题描述】:

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

【问题讨论】:

  • 您的预期输出是什么?
  • 您不会在每个运动循环中重置 $cats
  • 我需要具有与示例数组相同的输出,即运动数组,并且仅在该特定运动的类别内。
  • 你指的是reset($cats)吗?我应该在哪里称呼它?

标签: php arrays foreach associative-array


【解决方案1】:

您忘记重置 $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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2023-03-17
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多