2017-03-06 24 views
1

我有以下JSON輸出:如何選擇特定的JSON值來創建一個字符串?

"data" => "[ 
       {"month":2,"total_payments":"720.00"}, 
       {"month":3,"total_payments":"500.00"} 
      ]" 

我奮力的語法選擇一個特定的值,以創建一個字符串。例如,我希望能夠輸出

$month = ["2", "3"] 
$total_payments = ["720", "500"] 

到目前爲止,我有這樣的代碼,但不工作。

<?php echo $data->month?>

+0

您是否先解碼了json字符串? – bassxzero

+0

我在你的json中看到了語法錯誤。 – Xorifelse

+0

我在laravel中使用了toJson()方法返回 數組:1 [▼ 「data」=>「[{」month「:2,」total_payments「:」720.00「},{」month「:3, 「total_payments」:「500.00」}]「 ] –

回答

1

你必須遍歷陣列,並且獲取你想要的信息。例如:

$months = array(); 
$total_payments = array(); 
foreach($datas['data'] as $data) { 
    $months[] = $data->month; 
    $total_payments[] = $data->total_payments; 
} 
+0

您的'$ data =」「字符串」中有很好的語法錯誤「' – Xorifelse

+0

不,我是在說''data」=>「[{」month「'is a語法錯誤。使用'''來包裝該json。 – Xorifelse

+0

我收到非法字符串偏移'數據'錯誤? –

相關問題