here i have one array in this array i want to make json,i tried but i am unable to get the exact format what i want,any one please help me,
var_dump($absentresponse);
array(1) {
[0]=>
array(9) {
["absendId"]=>
string(1) "1"
["studentAbsentId"]=>
string(9) "["1","2"]"
["studentAbsentDate"]=>
string(10) "2017-04-11"
["schoolId"]=>
string(1) "2"
["classId"]=>
string(1) "1"
["sectionId"]=>
string(1) "1"
["reg_date"]=>
string(19) "2017-04-13 01:01:03"
["created_by"]=>
string(29) "kanniyappan@g2evolution.co.in"
["status"]=>
string(1) "0"
}
}
My expected results
{
"status": 1,
"data":
[
{
"studentabsentId":"1"
"studentAbsentDate": "2017-04-12"
},
{
"studentabsentId":"2"
"studentAbsentDate": "2017-04-12"
}
]
}
$json_response = json_encode($absentresponse); echo $json_response;
[
{
"absendId": "1",
"studentAbsentId": "[\"1\",\"2\"]",
"studentAbsentDate": "2017-04-11",
"schoolId": "2",
"classId": "1",
"sectionId": "1",
"reg_date": "2017-04-13 01:01:03",
"created_by": "kanniyappan@g2evolution.co.in",
"status": "0"
}
]
Please try below code:
$json_response = json_encode($absentresponse);
echo $json_response;
$absentresponse_json = json_encode($absentresponse);
Unless I'm not understanding your question, that should do it.
I think there is some wrong format in your array. Please recheck on index studentAbsentId
, I think that should be an array
, not a string. Below is my code if we assume that value of studentAbsentId
is an array of student ID.
$newArray = array();
$newArray['status'] = $absentresponse['status'];
$newArray['data'] = array();
foreach($absentresponse['studentAbsentId'] as $id){
$data['studentabsentId'] = $id;
$data['studentAbsentDate'] = $absentresponse['studentAbsentDate'];
$newArray['data'][] = $data;
}
$json = json_encode($newArray);
$absentresponse = array();
$data['status'] = 1;
foreach ($absentresponse as $key => $value) {
$row = array();
$row['absendId'] = $value['absendId'];
$row['studentAbsentId'] = $value['studentAbsentId'];
$row['studentAbsentDate'] = $value['studentAbsentDate'];
$row['schoolId'] = $value['schoolId'];
$row['classId'] = $value['classId'];
$row['sectionId'] = $value['sectionId'];
$row['reg_date'] = $value['reg_date'];
$row['created_by'] = $value['created_by'];
$data['data'][] = $row;
}
echo json_encode($data);