PHP convert JSON data using CakePHP -
PHP convert JSON data using CakePHP -
i have json
object generated through cakephp looks this:
[ {"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"}}]} ]
i combine each student
resulting json object looks this:
{"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"}
i advice in how improve cakephp info construction in order obtain desired result.
this stripping out of info in php array before encode it. consider approach:
$new_array = array( 'student' => array() ); foreach($current_array $current) { // take pupil 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"}]}
php json cakephp
Comments
Post a Comment