如何拆分和更改json格式

时间:2016-12-29 16:04:34

标签: php json slim

我正在使用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"
}];

有可能吗? 感谢

1 个答案:

答案 0 :(得分:2)

我不确定这是否与您的完全相同,但您可以通过将原始JSON转换为关联数组然后使用Foreach()循环重新构建每次迭代的数据来获得所需的JSON输出进入一个新的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()循环以引用assoc数组$gs。这应该有效,但如果没有,则使用$gs再次输出var_dump($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() .'}}';
        }
}