[Solved] How to group and create relationship from JSON response [closed]


It sounds like you want to group the items by league. Let’s use our array_reduce friend for that.
This is the basic syntax:

$arr = [
  [
    "leauge" => "sweeden",
    "fixture" => "12"
  ],
  [
    "leauge" => "sweeden",
    "fixture" => "13"
  ],
  [
    "leauge" => "germany",
    "fixture" => "14"
  ],
  [
    "leauge" => "france",
    "fixture" => "15"
  ],
];

$grouped = array_reduce($arr, function($agg, $item) {
  if (!isset($agg[$item['leauge']])) {
    $agg[$item['leauge']] = [];
  }
  $agg[$item['leauge']][] = $item;
  return $agg;
}, []);

print_r($grouped);

/*
// output:
Array
(
    [sweeden] => Array
        (
            [0] => Array
                (
                    [leauge] => sweeden
                    [fixture] => 12
                )

            [1] => Array
                (
                    [leauge] => sweeden
                    [fixture] => 13
                )

        )

    [germany] => Array
        (
            [0] => Array
                (
                    [leauge] => germany
                    [fixture] => 14
                )

        )

    [france] => Array
        (
            [0] => Array
                (
                    [leauge] => france
                    [fixture] => 15
                )

        )

)
*/

2

solved How to group and create relationship from JSON response [closed]