-1

I am trying to change an indexed array of arrays into a new array structure. My data is as follows:

$arr = array( 
     array( "year" => 1921, "name" => "bob" ),
     array( "year" => 1944, "name" => "steve" ),
     array( "year" => 1944, "name" => "doug" ),
     array( "year" => 1921, "name" => "jim" ),
);

I would like to recreate a new array thats groups them into the same year. The best I can come up with is designating the year as the key so any row that has that year gets added to that key, but it's not the output that I'm looking for. What I need is how it's listed below.

"data": [
    {
       "year":"1921",
       "names": [ 
            { "name":"bob" }, { "name":"jim"} 
        ]
    },
    {
        "year":1944",
        "names": [
            { "name":"steve" }, { "name":"doug "} 
        ]
    }
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
wuzz
  • 364
  • 3
  • 10
  • 1
    This question appears to be a "requirements dump". Please always post your best failed attempt to self-solve so that this community understands that you are not committing "help vampirism". By showing your faulty code, we can often avoid writing a complete script from scratch and simply pinpoint the part of your code that has gone awry. (adding proof of your toil also helps to prevent downV's) If you did code something up, it is never too late to add this to your question as an [edit]. – mickmackusa Oct 26 '20 at 22:48
  • @wuzz I was developing a complete example to answer https://stackoverflow.com/q/64635811/20860 when you deleted it. Can you undelete it please so I can share my answer? – Bill Karwin Nov 01 '20 at 19:38
  • 1
    @Bill Karwin undeleted – wuzz Nov 01 '20 at 19:47

1 Answers1

1

You don't need to pre-extract the unique years nor use conditions within nested loops. Just push the data into the result set using temporary first-level keys, then remove the temporary keys when finished looping.

This ensures unique years, but allows names to be duplicated.

Code: (Demo)

$arr = [
     ["year" => 1921, "name" => "bob"],
     ["year" => 1944, "name" => "steve"],
     ["year" => 1944, "name" => "doug"],
     ["year" => 1921, "name" => "jim"],
];

foreach ($arr as $item) {
    $result[$item['year']]['year'] = $item['year'];
    $result[$item['year']]['names'][] = ['name' => $item['name']];
}

echo json_encode(
         ['data' => array_values($result)],
         JSON_PRETTY_PRINT // for better visualization
);

Output:

{
    "data": [
        {
            "year": 1921,
            "names": [
                {
                    "name": "bob"
                },
                {
                    "name": "jim"
                }
            ]
        },
        {
            "year": 1944,
            "names": [
                {
                    "name": "steve"
                },
                {
                    "name": "doug"
                }
            ]
        }
    ]
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • You are correct, although the other way took me into the right direction and I was able to get it working, this is a much better solution in my opinion. My apologies. – wuzz Oct 26 '20 at 21:47