Skip to content

Instantly share code, notes, and snippets.

@flagoworld
Forked from anonymous/gist:5754885
Last active December 18, 2015 08:29
Show Gist options
  • Save flagoworld/5754891 to your computer and use it in GitHub Desktop.
Save flagoworld/5754891 to your computer and use it in GitHub Desktop.
<?php
//ARRAY IN:
$in=array(
array('name' => 'john', 'occupation' => 'electrician'),
array('name' => 'jack', 'occupation' => 'plumber'),
array('name' => 'jake', 'occupation' => 'office rat'),
array('name' => 'pete', 'occupation' => 'electrician')
);
//ARRAY OUT:
/*
array(
'electrician' => array('john', 'pete'),
'plumber' => array('jack'),
'office rat' => array('jake')
);
*/
$out=array();
foreach($in as $val)
{
if(!isset($out[$val['occupation']])&&!is_array($out[$val['occupation']]))
$out[$val['occupation']]=array();
$out[$val['occupation']][]=$val['name'];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment