Php 如何拆分和更改json格式

Php 如何拆分和更改json格式,php,json,slim,Php,Json,Slim,我使用的是SlimPHPv2,我有以下函数 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

我使用的是SlimPHPv2,我有以下函数

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转换为关联数组,然后使用
Foreach()
循环将每次迭代中的数据重新构造为新的assoc数组,从而获得所需的JSON输出。之后,您可以使用
JSON\u 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"
}]
输出:

$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()
循环以引用ASSOC数组
$gs
。这应该可以工作,但如果不行,则使用
var\u 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() .'}}';
        }
}

如果你注释掉我的代码和var_dump($gs);您得到JSON输出了吗?你能把JSON放在你的问题中吗(编辑任何机密的东西)我能看到问题是什么,你试图
JSON\u decode()
和不是JSON的对象。要进行测试,请添加
$gs=json\u encode($gs)
作为
if()
语句的第一行,对象随后将进行json_解码并应完成。显然,这不是一个解决方案,所以让我知道这是否有效。好的,让我试着更新我的答案以匹配您的代码。请参阅解释以获得充分的理解,并让我知道你们进展如何。是的,这样做很有魅力!非常感谢你!