为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕姐4208626 2023-09-22 15:27:00
我正在从数据库检索数据并将其推送到数组,然后使用 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”之后添加“项目”问候..
查看完整描述

2 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

$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);



查看完整回答
反对 回复 2023-09-22
?
慕莱坞森

TA贡献1810条经验 获得超4个赞

function get_first_property($object) {

    $properties = array_keys(get_object_vars($object));

    return reset($properties);

}

function object_merge($object1, $object2) {

    return (object) array_merge((array) $object1, (array) $object2);

}


// loop through each of the array objects

$previous_first_property = null;

foreach ($array as $i => $object) {

    $first_property = get_first_property($object);

    

    // merge if the 1st property is "items", and the previous 1st was "category_id"

    if ($first_property == "items" && $previous_first_property == "category_id") {

        $array[$i - 1] = object_merge($array[$i - 1], $array[$i]);

        unset($array[$i]);

    }

    

    $previous_first_property = $first_property;

}


查看完整回答
反对 回复 2023-09-22
  • 2 回答
  • 0 关注
  • 62 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信