簡體   English   中英

PHP 組 Json 對象

[英]PHP Group Json Objects

 "forms": [
            {
                "id": 1,
                "name": "new_patient",
                "deleted_at": null,
                "created_at": "2017-10-24 05:12:24",
                "updated_at": "2017-10-24 05:12:24",
                "pivot": {
                    "institution_id": 1,
                    "form_id": 1,
                    "field_name_id": 21,
                    "field_caption": "Gön.Hekim"
                }
            },
            {
                "id": 1,
                "name": "new_patient",
                "deleted_at": null,
                "created_at": "2017-10-24 05:12:24",
                "updated_at": "2017-10-24 05:12:24",
                "pivot": {
                    "institution_id": 1,
                    "form_id": 1,
                    "field_name_id": 22,
                    "field_caption": "Kurum"
                }
            },
            {
                "id": 1,
                "name": "new_patient",
                "deleted_at": null,
                "created_at": "2017-10-24 05:12:24",
                "updated_at": "2017-10-24 05:12:24",
                "pivot": {
                    "institution_id": 1,
                    "form_id": 1,
                    "field_name_id": 23,
                    "field_caption": "Endikasyon"
                }
            },
            {
                "id": 2,
                "name": "edit_patient",
                "deleted_at": null,
                "created_at": "2017-10-24 05:12:24",
                "updated_at": "2017-10-24 05:12:24",
                "pivot": {
                    "institution_id": 1,
                    "form_id": 2,
                    "field_name_id": 24,
                    "field_caption": "Materyal"
                }
            }
        ]

我想以相同的名稱/ ID 對表單對象進行分組。

我多么想要...

"forms": [
                {
                    "id": 1,
                    "name": "new_patient",
                    "deleted_at": null,
                    "created_at": "2017-10-24 05:12:24",
                    "updated_at": "2017-10-24 05:12:24",
                    "fields": [{
                        "field_name_id": 21,
                        "field_caption": "Gön.Hekim"
                    },{
                        "field_name_id": 22,
                        "field_caption": "Kurum"
                    },{
                        "field_name_id": 23,
                        "field_caption": "Endikasyon"
                    }]
                },
                {
                    "id": 2,
                    "name": "edit_patient",
                    "deleted_at": null,
                    "created_at": "2017-10-24 05:12:24",
                    "updated_at": "2017-10-24 05:12:24",
                    "fields": [{
                        "field_name_id": 24,
                        "field_caption": "Materyal"
                    }]
                }
            ]

我在so中搜索並用谷歌搜索,但我找不到任何解決我問題的方法。 有類似問題的答案。

我用 foreach 循環做了,但我認為在 php 中可能有一個方法。

而且,如果我在 json 中有一百多個對象,foreach 可能會很慢。

謝謝指教。

請嘗試以下操作:

$arr = json_decode($str, true);
$arr2 = [];
$ids = [];
foreach($arr['forms'] as $form)
{
    if(!in_array($form['id'], $ids))
    {
        $ids[] = $form['id'];
        $arr2[$form['id']] = $form;
    }
    unset($arr2[$form['id']]['pivot']);
    $arr2[$form['id']]['fields'][] = [$form['pivot']['field_name_id'], $form['pivot']['field_caption']];
}
$str2 = json_encode(['forms' => array_values($arr2)]);

echo $str2;

這使用foreach循環來獲取唯一的表單,並將創建一個新的鍵fields來保存field_name_idfield_caption並刪除pivot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM