如何拆分和更改 json 格式

How to split and change the json format

我正在使用 slimphp v2,我有以下功能

function gt($user) {
    $sql = "select id, id as categoryId, mobile, task from actions where status=0";
    try {
        $db = newDB($user);
        $stmt = $db - > prepare($sql);
        $stmt - > execute();
        $gs = $stmt - > fetchAll(PDO::FETCH_OBJ);
        if ($gs) {
            $db = null;
            echo json_encode($gs, JSON_UNESCAPED_UNICODE);
        } else {
            echo "Not Found";
        }
    } catch (PDOException $e) {
        echo '{"error":{"text":'.$e - > getMessage().
        '}}';
    }
}

默认 json 输出如下所示:

[{
    "id": "1",
    "categoryId": "1",
    "mobile": "111",
    "task": "test"
},
{
    "id": "2",
    "categoryId": "2",
    "mobile": "222",
    "task": "test2"
}]

以及我正在尝试制作的输出。我需要生成一个 ID:1_(id) 然后像这样格式化它

[{
    id: "1",
    task: "test",
}, {
    ID: "1_1", //generate this, add 1_id
    categoryId: "1",
    mobile: "00000",
},  {
    id: "2",
    task: "test2",
}, {
    ID: "1_2", //generate this, add 1_id
    categoryId: "2",
    mobile: "11111"
}];

可能吗? 谢谢

我不确定这是否正是您想要的,但您可以通过将原始 JSON 转换为关联数组然后在每次迭代中重组数据来获得所需的 JSON 输出使用 Foreach() 循环进入新的 assoc 数组。之后,您可以使用 json_encode().

将其转换回 JSON

代码:

$json = '[{
    "id": "1",
    "categoryId": "1",
    "mobile": "111",
    "task": "test"
},
{
    "id": "2",
    "categoryId": "2",
    "mobile": "222",
    "task": "test2"
}]';

$jsonArr = json_decode($json, TRUE);
$newArr = [];

foreach ($jsonArr as $v) {

    $newArr[] = ['id:'=>$v['id'], 'task:' => $v['task']];
    $newArr[] = ['ID:'=>'1_' . $v['id'], 'categoryId' => $v['categoryId'], 'mobile'=>$v['mobile']];  

}

$newJson = json_encode($newArr);
var_dump($newJson);

输出:

[{
    "id:": "1",
    "task:": "test"
}, {
    "ID:": "1_1",
    "categoryId": "1",
    "mobile": "111"
}, {
    "id:": "2",
    "task:": "test2"
}, {
    "ID:": "1_2",
    "categoryId": "2",
    "mobile": "222"
}]

编辑 -- 更新答案

如评论中所述,您将 SQL 数组作为对象输出。我已经使用 PDO::FETCH_ASSOC 将 Fetch 设置为作为关联数组输出,并更改了 foreach() 循环以引用关联数组 $gs。这应该有效,但如果无效,则使用 var_dump($gs) 再次输出 $gs 的结果。如果需要,您仍然需要编码为 JSON,但此行已被注释掉。

function gt($user) {
    $sql = "select id, id as categoryId, mobile, task from actions where status=0";
        try {
                $db = newDB($user);
                $stmt = $db->prepare($sql);
                $stmt->execute();
                $gs = $stmt->fetchAll(PDO::FETCH_ASSOC); //Fetch as Associative Array

                if ($gs) {

                    $db = null;
                    $newArr = [];

                    foreach ($gs as $v) { //$gs Array should still work with foreach loop

                        $newArr[] = ['id:'=>$v['id'], 'task:' => $v['task']];
                        $newArr[] = ['ID:'=>'1_' . $v['id'], 'categoryId' => $v['categoryId'], 'mobile'=>$v['mobile']];

                    }

                    //$newJson = json_encode($newArr); //JSON encode here if you want it converted to JSON.




                } else {

                 echo "Not Found";

                }

        } catch(PDOException $e) {

            //error_log($e->getMessage(), 3, '/var/tmp/php.log');
                echo '{"error":{"text":'. $e->getMessage() .'}}';
        }
}