2016-11-18 66 views
-1

我有輸出下面的JSON結果的代碼:Laravel - 格式JSON

{ 
    "Ghost in the Shell": { 
     "id": 1203, 
     "Pipeline": { 
      "id": 6144, 
      "name": "Pipeline", 
      "status": "New", 
      "item_id": 5962, 
      "TotalTasksOpen": 2 
     } 
    } 
} 

現在我怎麼可以在下面的我需要的JSON格式格式化:

注*我不得不刪除一些結果。

{ 
    "Ghost in the Shell": 
    "id": 6144, 
    "name": "Pipeline", 
    "status": "New", 
    "item_id": 5962, 
    "TotalTasksOpen": 2 
} 

如果有人能幫助我,請大家欣賞。

+4

這不是* JSON格式*,但*適應數據結構*你需要的方式。您只需在'json_encode'位之前更改您的代碼。但是,我們不能幫助你,因爲你沒有提供**代碼。 –

+1

(更不用說你想要的格式是*不是*有效的JSON) –

回答

0

我不知道你是什麼目的,但這裏是你需要

<?php 

$json = '{ 
    "Ghost in the Shell": { 
     "id": 1203, 
     "Pipeline": { 
      "id": 6144, 
      "name": "Pipeline", 
      "status": "New", 
      "item_id": 5962, 
      "TotalTasksOpen": 2 
     } 
    } 
}'; 

$array = json_decode($json, true); 

$newArray = array(); 
foreach($array as $key=>$value) { 

    $newArray[$key] = ''; 
    foreach($value as $k=>$v) { 

     if(is_array($v)) { 

     $newArray = array_merge($newArray,$v); 
     } 
    } 
} 

$newJson = json_encode($newArray); 
echo $newJson; 

?>