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



PHP Snippet 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'];
}

PHP Snippet 2:

$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);

PHP Snippet 3:

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
                )

        )

)

PHP Snippet 4:

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