2016-12-29 66 views
1

我使用slimphp v2和我有以下功能如何分割和改變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_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" 
}]; 

這可能嗎? 感謝

回答

2

我不知道如果這是你可以通過轉換原始的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" 
}] 

編輯 - 更新了答案

正如在評論中討論,你的輸出你的S QL數組作爲對象。我已經使用PDO::FETCH_ASSOC將提取設置爲關聯數組輸出,並將foreach()循環更改爲引用assoc數組$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() .'}}'; 
     } 
} 
+0

我上面的代碼顯示錯誤或? – Kitson88

+0

它顯示一個錯誤500. http://pastebin.com/cmD4cqYB – UrL

+0

如果你註釋掉我的代碼和var_dump($ gs);你有JSON輸出嗎?你可以把JSON放在你的問題中(編輯任何保密的東西) – Kitson88