I’m having problems grouping arrays based on the value of an item within an array.
I have the following array which is generated by joining 3 tables via SQL:
Array (
[0] => stdClass Object ( [company_id] => 1 [company_name] => My Company [user_id] => 4 [user_email] => toni.t@gmail.com [user_username] => toni [meta_name] => Toni [meta_surname] => Trivkovic [meta_company_id] => 1 )
[1] => stdClass Object ( [company_id] => 2 [company_name] => Ignited Labs [user_id] => 5 [user_email] => nicolo.v@gmail.com [user_username] => nick [meta_name] => Nicolo [meta_surname] => Volpato [meta_company_id] => 2 )
[2] => stdClass Object ( [company_id] => 2 [company_name] => Ignited Labs [user_id] => 6 [user_email] => john.smith@gmail.com [user_username] => john [meta_name] => John [meta_surname] => Smith [meta_company_id] => 2 )
)
What I would like to do is reconstruct this array so users under the same company are grouped within a company’s array. In this example nick and john would be grouped under one array (My Company) and toni would be grouped in another.
I’m aware I would probably have to use the array_push() function within a loop but I’m having issues getting this code working. Does anyone have any samples of code or can someone provide me an example of how to achieve this?