我有一个巨大的 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"
}
}
}
}
}
我添加了一个嵌套的
uasort
函数来根据“order”属性对每个部分中的字段进行排序,因此它将确保您的部分和每个部分中的字段都正确排序,并且还要注意 toArray
方法不需要接受 Request
对象作为参数,您可以简单地使用 $request
代替!
class FieldSettingResource extends JsonResource
{
public function toArray($request)
{
$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'];
});
// Sort the fields within each section based on the 'order' attribute
foreach ($sections as $key => $value) {
uasort($sections[$key]['fields'], 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,
];
}
}