我有一个uploads
看起来像这样的数组:
$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');
我想创建这个数组:
$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')
)
);
基本上我想根据它们的标签对数组进行分组。要分组的数组是动态的,否则我可以执行以下操作:
$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
);
但是由于键是动态的,我该如何实现呢?