我有一个巨大的jsonb对象,我想按order
为每个子部分排序
以section_
开始的每个部分具有order
密钥。
这是我在资源中尝试的,但它无法排序
class FieldSettingResource extends JsonResource
{
public function toArray(Request $request): array
{
$fieldData = $this->field_data;
$sections = [];
foreach ($fieldData as $key => $value) {
if (strpos($key, 'section_') === 0) {
$sections[$key] = $value;
}
}
// Sort the sections based on the 'order' attribute
uasort($sections, function ($a, $b) {
return $a['order'] <=> $b['order'];
});
// Overwrite original sections with sorted sections in the fieldData
foreach ($sections as $key => $value) {
$fieldData[$key] = $value;
}
return [
'id' => (string)$this->id,
'name' => $this->name,
'field_data' => $fieldData,
];
}
}
这里有一个对象的例子
{
"tab_patient": {
"label": "patient",
"show": true,
"section_general_info": {
"order": 1,
"label": "generalInfo",
"show": true,
"fields": {
"hospital_number": {
"id":1,
"show": true,
"mandatory": false,
"label": "hospitalNumber"
},
"nhs_number": {
"id":2,
"show": true,
"mandatory": true,
"label": "nhsNumber"
}
}
},
"section_other_info": {
"order": 2,
"label": "otherInfo",
"show": true,
"fields": {
"date_of_birth": {
"id":1,
"show": true,
"mandatory": false,
"label": "dateOfBirth"
},
"age": {
"id":2,
"show": false,
"mandatory": true,
"label": "ageAtAdmission",
"translationTab":"patientTab"
}
}
},
"section_next_of_kin": {
"order": 3,
"label": "nextOfKin",
"show": true,
"fields": {
"nok_name": {
"id":1,
"show": true,
"mandatory": false,
"label": "name",
"translationTab":"patientTab"
},
"nok_relation": {
"id":2,
"show": false,
"mandatory": true,
"label": "relation",
"translationTab":"patientTab"
}
}
}
},
"tab-admission": {
"label": "admission",
"show": true,
"section_referral_info": {
"order": 1,
"label": "referralInfo",
"show": true,
"fields": {
"type": {
"id":1,
"show": true,
"mandatory": false,
"label": "type",
"translationTab":"admissionTab"
},
"id_number": {
"id":2,
"show": true,
"mandatory": true,
"label": "idNumber",
"translationTab":"admissionTab"
}
}
},
"section_call_details": {
"order": 2,
"label": "callDetails",
"show": true,
"fields": {
"situation": {
"id":1,
"show": true,
"mandatory": false,
"label": "situation",
"translationTab":"admissionTab"
},
"referrer": {
"id":2,
"show": true,
"mandatory": false,
"label": "referrer",
"translationTab":"admissionTab"
}
}
}
}
}
1条答案
按热度按时间z31licg01#
这工作