I have an array that looks something like this:
$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',
],
];
I would like to sort this array in such a way where all the fields of type fieldset
are at the bottom. At the same time, I would like the text fields (which are on top) to be sorted alphabetically by the #label
and the fieldsets (which should be at the bottom) should also be sorted alphabetically by the label. This is what I had so far.
ksort($array);
uasort($array, function($field1) {
if ($field1['#type'] !== 'fieldset') {
return 0;
}
return 1;
});
Sorting the array by the key value successfully sorts my entire array alphabetically. But once I add the uasort
, although my fieldset
's are at the bottom, the alphabetical order is no longer present.
Any ideas?