2016-11-02 116 views
2

我有來自角度前端的JSON響應。答案是:如何在Laravel中獲取Json數據

{ 
    "selected": null, 
    "lists": [ 
    { 
     "board one": [ 
     { 
      "card_name": "Card 10", 
      "id": 1, 
      "card_unique_id": "PC1" 
     }, 
     { 
      "card_name": "card 11", 
      "id": 2, 
      "card_unique_id": "PC2" 
     }, 
     { 
      "card_name": "card12", 
      "id": 3, 
      "card_unique_id": "PC3" 
     }, 
     { 
      "card_name": "adfaf", 
      "id": 12, 
      "card_unique_id": "PC12" 
     }, 
     { 
      "card_name": "fa df af ad", 
      "id": 13, 
      "card_unique_id": "PC13" 
     } 
     ], 
     "BoardId": "PB1" 
    }, 
    { 
     "board two": [ 
     { 
      "card_name": "Card 20", 
      "id": 4, 
      "card_unique_id": "PC4" 
     }, 
     { 
      "card_name": "CArd 21", 
      "id": 5, 
      "card_unique_id": "PC5" 
     }, 
     { 
      "card_name": "card 23", 
      "id": 6, 
      "card_unique_id": "PC6" 
     }, 
     { 
      "card_name": "Card 24", 
      "id": 7, 
      "card_unique_id": "PC7" 
     } 
     ], 
     "BoardId": "PB2" 
    }, 
    { 
     "board three": [ 
     { 
      "card_name": "Card 30", 
      "id": 8, 
      "card_unique_id": "PC8" 
     }, 
     { 
      "card_name": "card 31", 
      "id": 9, 
      "card_unique_id": "PC9" 
     }, 
     { 
      "card_name": "card 32", 
      "id": 10, 
      "card_unique_id": "PC10" 
     } 
     ], 
     "BoardId": "PB3" 
    }, 
    { 
     "new board": [ 
     { 
      "card_name": "card 40", 
      "id": 11, 
      "card_unique_id": "PC11" 
     } 
     ], 
     "BoardId": "PB4" 
    } 
    ] 
} 

我想在laravel中獲取所有卡的名稱。在laravel控制器函數中,我的代碼如下所示:它返回一些錯誤。我不知道如何獲取所有這些card_names

public function sortupdate(Request $response) 
    { 
     $get_result_arr = json_decode($response->getContent()); 

     foreach($get_result_arr as $result){ 
      $lists = $result->lists; 
     } 
    } 

請任何人幫忙。 感謝高級..

+0

請上的錯誤更具體返回 – Wistar

+0

我不知道如何獲取。請回答。謝謝Wistar – SARAN

回答

2

添加truejson_decode()得到一個關聯數組:

$get_result_arr = json_decode($response->getContent(), true); 

然後在這個陣列進行迭代,並得到各card_name

foreach ($get_result_arr['lists'] as $list) { 
    foreach (array_values($list)[0] as $card) { 
     echo $card['card_name']; 
    } 
} 
+0

謝謝Alexey Mezenin。但它返回未定義的索引:列表。 – SARAN

+0

$ get_result_arr返回數組。但返回錯誤爲未定義的索引列表 – SARAN

+0

@SARAN,我已經更新了代碼並進行了測試。它與您的數據完美配合。 –