2016-11-18 45 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; 

?>