-2

I am trying to group JSON data from API response and create a relationship from it. The data I am receiving is the statistics from live football games. The response includes arrays of live fixtures. Each fixture contains data about the leagues and teams. Different fixtures may be from the same league. Now I want to retrieve the leagues and group fixtures from the same league together while maintaining the league-fixture relationship. In a way, I can query league->fixtures() and get all fixtures under the league. Here is a sample response from the API enter image description here

Please help if you have a hint on how to achieve that. Thank in advance

Tula
  • 189
  • 1
  • 1
  • 7
  • Perhaps if you could convert this into a collection (`collect($responseArray)`), this would help I guess. https://stackoverflow.com/a/72160582/3532758 – user3532758 Sep 05 '22 at 17:54
  • I am so lost with what you want, what you have and more... please edit your question and add an example result of what are you expecting back... Learn [ask] – matiaslauriti Sep 05 '22 at 18:36
  • Please never, ever present your array data as a screenshot. We prefer that PHP arrays are the presented in the question body as the copy-pasted text from a `var_export()` call. `dd()` is not a usable format for volunteers. – mickmackusa Sep 05 '22 at 21:52

1 Answers1

1

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
                )

        )

)
*/
IT goldman
  • 14,885
  • 2
  • 14
  • 28
  • Would be awesome if you use Collections, most people will understand it even more because it is a Laravel question, still the author did not specify what he has, so we are just guessing, still +1 – matiaslauriti Sep 05 '22 at 18:37
  • `if (!isset($agg[$item['leauge']])) { $agg[$item['leauge']] = []; }` is not necessary in PHP. Parent elements do not need to be declared before pushing child elements using square brace syntax. If this answer is the correct solution, then this question is definitely a duplicate and should have been closed instead of answered. [How to group subarrays by a column value?](https://stackoverflow.com/q/12706359/2943403) – mickmackusa Sep 05 '22 at 21:55