'Combine multiple columns into an array as one of the key in a results set

I am having trouble placing data into its right structure. I have a result set data of:

array (
  0 => 
  array (
    'company_id' => 1,
    'company_abbrev' => 'amd',
    'description' => 'amd blah blah blah 1',
  ),
  1 => 
  array (
    'company_id' => 1,
    'company_abbrev' => 'amd',
    'description' => 'amd blah blah blah 2',
  ),
  2 => 
  array (
    'company_id' => 2,
    'company_abbrev' => 'int',
    'description' => 'int blah blah 1',
  ),
  3 => 
  array (
    'company_id' => 2,
    'company_abbrev' => 'int',
    'description' => 'int blah blah 2',
  ),

I am trying to combine the description into its own array within each company in is single, unique entry:

array (
  'amd' => 
  array (
    'company_id' => 1,
    'job_description' => 
    array (
      0 => 'amd blah blah 1',
      1 => 'amd blah blah 2',
    ),
  ),

The loop that I am have it in is piling all the job descriptions in each different companies as it gets to the very last company. What am I doing wrong?

for($i = 0; $i < sizeof($comp); $i++) {
            $companies[$comp[$i]['company_abbrev']] = $comp[$i];
            $array[] = $comp[$i]['description'];
            $companies[$comp[$i]['company_abbrev']]['job_description'] = $array;
}


Solution 1:[1]

foreach($comp as $com) {
    if(!isset($companies[$com['company_abbrev']])) {
        $companies[$com['company_abbrev']] = array();
        $companies[$com['company_abbrev']]['company_id'] = $com['company_id'];
    }

    $companies[$com['company_abbrev']]['description'][] = $com['description'];
}

this loop groups it using the company abbreviation

Solution 2:[2]

You can walk over the array like this below,

$res = [];
array_walk($arr, function (&$val, $key) use (&$res) {
    $res[$val['company_abbrev']]['company_id']        = $val['company_id']; // setting company id
    $res[$val['company_abbrev']]['job_description'][] = $val['description']; // AUTOMATIC mapping
});
print_r($res);

(Output)

Array
(
    [amd] => Array
        (
            [company_id] => 1
            [job_description] => Array
                (
                    [0] => amd blah blah blah 1
                    [1] => amd blah blah blah 2
                )

        )

    [int] => Array
        (
            [company_id] => 2
            [job_description] => Array
                (
                    [0] => int blah blah 1
                    [1] => int blah blah 2
                )

        )

)

(Demo).

Solution 3:[3]

It is not necessary to check if subarrays are already declared (or to declare them as an empty array) before pushing data into them.

Use extract() to create simple variables instead of crowding the script with lots of square braces.

I added an s to the end of job_description because pluralization better represents the data.

Whenever using extract() be careful that keys being translated into variables will not interfere with other previously declared variable in the same scope.

Code: (Demo)

$result = [];
foreach ($array as $row) {
    extract($row);
    $result[$company_abbrev]['company_id'] = $company_id;
    $result[$company_abbrev]['job_descriptions'][] = $description;
}
var_export($result);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Festus Yuma
Solution 2 Rahul
Solution 3 mickmackusa