我有一个看起来像这样的数组:
$array = [
'field1' => [
'#type' => 'text',
'#label' => 'Field 11',
],
'field4' => [
'#type' => 'fieldset',
'#label' => 'Field 4',
],
'field12' => [
'#type' => 'text',
'#label' => 'Field 12',
],
'field3' => [
'#type' => 'fieldset',
'#label' => 'Field 3',
],
'field18' => [
'#type' => 'text',
'#label' => 'Field 18',
],
];
我想以所有类型字段fieldset
都在底部的方式对这个数组进行排序。同时,我希望文本字段(位于顶部)按字母顺序排序,字段集(#label
应位于底部)也应按标签字母顺序排序。这是我到目前为止所拥有的。
ksort($array);
uasort($array, function($field1) {
if ($field1['#type'] !== 'fieldset') {
return 0;
}
return 1;
});
按键值对数组进行排序成功地按字母顺序对我的整个数组进行排序。但是一旦我添加了uasort
,尽管我fieldset
的 位于底部,但字母顺序不再存在。
有任何想法吗?
使用 usort 有 2 个条件
usort($array, function ($i1, $i2) {
// compare types
$r = ($i1['#type'] == 'fieldset' ? 1 : 0) - ($i2['#type'] == 'fieldset' ? 1 : 0);
// if both are (or not) fieldset (r == 0), compare labels
return $r ? $r : strcmp($i1['#label'], $i2['#label']); } );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句