0

I need help in order to manipulate a multidimensional array during PHP foreach loop. I have the array below:

$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"
    ],
];

I have the below foreach in order to take all the sports with the corresponding categories for every sport,

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;
}

but the output is not what I expected; instead, I am getting repeated results like the one below:

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"
    }
  }
}

As you can imagine this is not the correct output, as in the categories of the tennis you can see the categories of the soccer also. How can I correct my foreach loop in order to get the correct output?

4
  • what is your expected output? Commented Dec 22, 2017 at 10:26
  • You're not resetting $cats on each sport loop. Commented Dec 22, 2017 at 10:27
  • I need to have the same output as the example array, that is an array of sports and inside the categories for this specific sport only. Commented Dec 22, 2017 at 10:28
  • Are you referring to reset($cats)? and where should I call it? Commented Dec 22, 2017 at 10:29

1 Answer 1

4

You forget to reset $cats and $data arrays.

<?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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.