I have an uploads
array that looks like this:
$uploads[0] = array('type' => 'upload', 'label' => 'blah');
$uploads[1] = array('type' => 'upload', 'label' => 'blah');
$uploads[2] = array('type' => 'upload', 'label' => 'blah');
$uploads[3] = array('type' => 'upload', 'label' => 'foobar');
I want to create this array:
$upload_fields = array(
'blah' => array(
array('type' => 'upload', 'label' => 'blah')
array('type' => 'upload', 'label' => 'blah')
array('type' => 'upload', 'label' => 'blah')
),
'foobar' => array(
array('type' => 'upload', 'label' => 'foobar')
)
);
Basically I want to group the arrays based on their labels. The arrays to be grouped are dynamic otherwise I could do something like:
$array_group_1 = array();
$array_group_2 = array();
foreach($uploads as $upload) {
if($upload['label'] == 'blah') {
array_push($array_group_1, $upload);
} else {
array_push($array_group_2, $upload);
}
}
$upload_fields = array(
'blah' => $array_group_1,
'foobar' => $array_group_2
);
But since the keys are dynamic how can I achieve this?
This is what I came up with:
$uploads[0] = array('type' => 'upload', 'label' => 'blah');
$uploads[1] = array('type' => 'upload', 'label' => 'blah');
$uploads[2] = array('type' => 'upload', 'label' => 'blah');
$uploads[3] = array('type' => 'upload', 'label' => 'foobar');
foreach($uploads as $u){
$upload_fields[$u['label']][] = $u;
}
echo '<pre>',print_r($upload_fields),'</pre>';