猿问

是否可以编辑 json 数组的元素来容纳其他数据/元素?

我正在从数据库检索数据并将其推送到数组,然后使用 json_encode() 以 json 格式输出。我最终得到了这种格式的数据。


[

    {

        "category_id": "1",

        "category_name": "Construction Materials"

    },

    {

        "items": [

            {

                "id": "1",

                "item_name": "Wire Mesh",

                "price": "459",

                "image_url": null

            },

            {

                "id": "2",

                "item_name": "Cement",

                "price": "700",

                "image_url": null

            },

            {

                "id": "3",

                "item_name": "Barbed Wire",

                "price": "3000",

                "image_url": null

            },

            {

                "id": "4",

                "item_name": "Iron sheet",

                "price": "200",

                "image_url": null

            }

        ]

    },

    {

        "category_id": "2",

        "category_name": "Plumbing"

    },

这是我想要实现的目标:


[

    {

        "category_id": "1",

        "category_name": "Construction Materials"

        "items": [

            {

                "id": "1",

                "item_name": "Wire Mesh",

                "price": "459",

                "image_url": null

            },

            {

                "id": "2",

                "item_name": "Cement",

                "price": "40",

                "image_url": null

            },

            {

                "id": "3",

                "item_name": "Barbed Wire",

                "price": "3000",

                "image_url": null

            },

            {

                "id": "4",

                "item_name": "Iron sheet",

                "price": "200",

                "image_url": null

            }

        ]

    },

    {

        "category_id": "2",

        "category_name": "Plumbing"

    },

我怎样才能在 php.ini 中实现这一点?是否可以编辑主数组的内容?如何在“category_name”之后添加“项目”问候..


慕姐4208626
浏览 102回答 2
2回答

幕布斯7119047

$array = json_decode($yourJson);$arrayLength = count($array);$finalArray = [];for ($i=0; $i < $arrayLength; $i+=2) {    $finalArray[] = $array[$i] + $array[$i + 1];}$backToJson = json_encode($finalArray);

慕莱坞森

function get_first_property($object) {&nbsp; &nbsp; $properties = array_keys(get_object_vars($object));&nbsp; &nbsp; return reset($properties);}function object_merge($object1, $object2) {&nbsp; &nbsp; return (object) array_merge((array) $object1, (array) $object2);}// loop through each of the array objects$previous_first_property = null;foreach ($array as $i => $object) {&nbsp; &nbsp; $first_property = get_first_property($object);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // merge if the 1st property is "items", and the previous 1st was "category_id"&nbsp; &nbsp; if ($first_property == "items" && $previous_first_property == "category_id") {&nbsp; &nbsp; &nbsp; &nbsp; $array[$i - 1] = object_merge($array[$i - 1], $array[$i]);&nbsp; &nbsp; &nbsp; &nbsp; unset($array[$i]);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $previous_first_property = $first_property;}
随时随地看视频慕课网APP
我要回答