2014-10-10 27 views
-4

PHP轉換JSON數據我現在有通過CakePHP的產生JSON物體看起來像這樣:使用CakePHP

[ 
{"Student":{"id":"1","name":"Pov Phearom","gender":"Male","address":"BTB","phone":"0986865898","country_id":"1"},"Country":{"id":"1","name":"Cambodia"},"Course":[]}, 
{"Student":{"id":"2","name":"Met Mok","gender":"Male","address":"Btb","phone":"09938273","country_id":"1"},"Country":{"id":"1","name":"Cambodia"},"Course":[]}, 
{"Student":{"id":"3","name":"Ovb Vannak","gender":"Male","address":"Bt","phone":"09998786","country_id":"2"},"Country":{"id":"2","name":"China"},"Course":[{"id":"2","name":"Java","description":"s","StudentsCourse":{"id":"9","student_id":"3","course_id":"2"}},{"id":"3","name":"VB","description":"s","StudentsCourse":{"id":"10","student_id":"3","course_id":"3"}},{"id":"5","name":"Android Programming","description":"Mobile developement tools","StudentsCourse":{"id":"13","student_id":"3","course_id":"5"}}]},{"Student":{"id":"4","name":"Meas Sovannara","gender":"Male","address":"Battambang","phone":"098637273","country_id":"1"},"Country":{"id":"1","name":"Cambodia"},"Course":[{"id":"2","name":"Java","description":"s","StudentsCourse":{"id":"11","student_id":"4","course_id":"2"}},{"id":"3","name":"VB","description":"s","StudentsCourse":{"id":"14","student_id":"4","course_id":"3"}},{"id":"5","name":"Android Programming","description":"Mobile developement tools","StudentsCourse":{"id":"12","student_id":"4","course_id":"5"}}]}, 
{"Student":{"id":"5","name":"Dara Kon","gender":"Male","address":"Battambang","phone":"098285326","country_id":"1"},"Country":{"id":"1","name":"Cambodia"},"Course":[{"id":"2","name":"Java","description":"s","StudentsCourse":{"id":"16","student_id":"5","course_id":"2"}},{"id":"3","name":"VB","description":"s","StudentsCourse":{"id":"17","student_id":"5","course_id":"3"}},{"id":"5","name":"Android Programming","description":"Mobile developement tools","StudentsCourse":{"id":"15","student_id":"5","course_id":"5"}}]} 
] 

我想結合各Student因此產生的JSON對象是這樣的:

{"Student":[ 
{"id":"1","name":"Pov Phearom","gender":"Male","address":"BTB"}, 
{"id":"2","name":"Met Mok","gender":"Male","address":"Btb"}, 
{"id":"3","name":"Ovb Vannak","gender":"Male","address":"Bt"}, 
{"id":"4","name":"Meas Sovannara","gender":"Male","address":"Battambang"}, 
{"id":"5","name":"Dara Kon","gender":"Male","address":"Battambang"} 
],"success":1,"message":"successfull"} 

我想得到一些建議,我如何改進我的CakePHP數據結構以獲得所需的結果。

+1

你做了什麼來實現這一目標? – JakeGould 2014-10-10 01:23:06

回答

0

這只是在你編碼之前剝離你的PHP數組中的一些數據。考慮這樣的方法:

$new_array = array(
    'Student' => array() 
); 

foreach($current_array as $current) { 
    // Just take the student part 
    $new_array['Student'][] = $current['Student']; 
} 

echo json_encode($new_array); 

Example:

{"Student":[{"id":"1","name":"Pov Phearom","gender":"Male","address":"BTB","phone":"0986865898","country_id":"1"},{"id":"2","name":"Met Mok","gender":"Male","address":"Btb","phone":"09938273","country_id":"1"},{"id":"3","name":"Ovb Vannak","gender":"Male","address":"Bt","phone":"09998786","country_id":"2"},{"id":"4","name":"Meas Sovannara","gender":"Male","address":"Battambang","phone":"098637273","country_id":"1"},{"id":"5","name":"Dara Kon","gender":"Male","address":"Battambang","phone":"098285326","country_id":"1"}]} 
+0

我已經做到了。它工作得很好。謝謝 – 2014-10-10 01:54:26

+0

如果我想再獲得一個字段是country_name。我能怎麼做? – 2014-10-10 02:02:52

+0

如果你想把它作爲同一個數組的一部分(即平坦記錄) - 做'array_merge($ current ['Student'],array('country_name'=> $ current ['Country'] ['name']) )'在你的任務中 – 2014-10-10 03:49:13

0

我沒有用過蛋糕。但假設你有你的數據查詢結果在一個關聯的數組... ...例如:

$oneRow["id"] = 1; 
$oneRow["name"] = "Don"; 
$rows[] = $oneRow; 
$oneRow["id"] = 2; 
$oneRow["name"] = "Bill"; 
$rows[] = $oneRow; 
// above is just an example, usually $rows would be a query result. 

// You can return the json you want with something like this: 
$dataToReturn["Student"] = $rows; 
$dataToReturn["success"] = 1; 
$dataToReturn["messages"] = "Successful"; 
$sendThisBack = json_encode($dataToReturn); 
0

我只是想添加到您的答案。 @ user3163976通過執行json_encode()將數組轉換爲json對象之前, 在該數組中執行此操作

此處的數據是一個數組,其中包含您隨身攜帶的值。

 $data = Hash::extract($data,'{n}.Student'); 
     $data = array('Student' => $data); 
     $data = json_encode($data); 
     debug($data); 

而你的結果應該是一樣的。 你可以閱讀所有關於這裏 Hash - CakePHP Core utility

玩得開心CakePHP的核心工具;)