按子键对Laravel jsonb排序

sy5wg1nm  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(79)

我有一个巨大的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"
                    }
                }

            }
        }
    }
z31licg0

z31licg01#

这工作

class FieldSettingResource extends JsonResource
{

public function toArray(Request $request): array
{
    $sortedFieldData = $this->sortFieldData($this->field_data);
    return [
        'id'          => (string)$this->id,
        'name'        => $this->name,
        'field_data'  => $sortedFieldData
    ];
}

private function sortFieldData(array $fieldData): array
{
    foreach ($fieldData as $mainSection => &$subSections) {
        if (is_array($subSections) && strpos($mainSection, 'tab') !== false) {
            uasort($subSections, function ($a, $b) {
                // Check for the existence of the 'order' key before comparing
                if (is_array($a) && isset($a['order']) && is_array($b) && isset($b['order'])) {
                    return $a['order'] <=> $b['order'];
                }
                return 0; // If the 'order' key doesn't exist, consider them equal for sorting purposes
            });
        }
    }

    return $fieldData;
}
}

相关问题