How to display the json data within array?

My code for extracting the data is as follows:

$query = mysqli_query($con,"SELECT `item_id` ,  `shop_id` ,  `item_list_date`   FROM  `item_list`  WHERE `yb_id` = '$yb_id'");
//echo "SELECT `item_id` ,  `shop_id` ,  `item_list_date`   FROM  `item_list`  WHERE `yb_id` = '$yb_id'";
if(mysqli_affected_rows($con)>0)
{
while ($row2 = mysqli_fetch_object($query))
{
    $data1[] = $row2;    
}
//}

//"SELECT  `item_list_details`.`item_id` ,  `item_name` ,  `item_qty` ,  `item_unit` ,  `item_detail` ,  `item_flag` FROM  `item_list_details` LEFT JOIN  `item_list` ON  `item_list`.`item_id` =  `item_list_details`.`item_id` WHERE  `item_list`.`item_id` =50 AND  `yb_id` =  '70380KTXDY'";


$query = mysqli_query($con,"SELECT  `item_list_details`.`item_id` ,  `item_name` ,  `item_qty` ,  `item_unit` ,  `item_detail` ,  `item_flag` FROM  `item_list_details` LEFT JOIN  `item_list` ON  `item_list`.`item_id` =  `item_list_details`.`item_id` WHERE  `item_list`.`yb_id` =  '$yb_id'");
//echo "SELECT  `item_list_details`.`item_id` ,  `item_name` ,  `item_qty` ,  `item_unit` ,  `item_detail` ,  `item_flag` FROM  `item_list_details` LEFT JOIN  `item_list` ON  `item_list`.`item_id` =  `item_list_details`.`item_id` WHERE  `item_list`.`yb_id` = '$yb_id'";
if(mysqli_affected_rows($con)>0)
{
while ($row2 = mysqli_fetch_object($query))
{
    $data2[] = $row2;    

}
}
$data['errorcode'] = 45000;
$data['list_details'] = $data1;
$data['item_list_details'] = $data2;
$data4['details'] = $data;
//echo json_encode($data3);
//}
}
else
   {
       $data['errorcode'] = 45001;
       
//       $data['details']=$data5;
    //echo'You entered username or password is incorrect';
   }
   echo json_encode($data4);

Open in new window


Output that I'm getting is:

{"details":{"errorcode":45000,"list_details":[{"item_id":"49","shop_id":"1","item_list_date":"2017-12-20"},{"item_id":"67","shop_id":"1","item_list_date":"2017-12-04"},{"item_id":"92","shop_id":"1","item_list_date":"2017-12-12"},{"item_id":"93","shop_id":"1","item_list_date":"2017-12-12"}],"item_list_details":[{"item_id":"92","item_name":"chocolates","item_qty":"1","item_unit":"gms","item_detail":"1 packet","item_flag":"0"},{"item_id":"92","item_name":"qwerty","item_qty":"2","item_unit":"2","item_detail":"rice flour","item_flag":"0"},{"item_id":"93","item_name":"chocolates","item_qty":"1","item_unit":"gms","item_detail":"1 packet","item_flag":"0"},{"item_id":"93","item_name":"qwerty","item_qty":"2","item_unit":"2","item_detail":"rice flour","item_flag":"0"}]}}

Open in new window


But I want the output in the below format, so how do I do that?
{
"Item_Response" :
{ 
“errorcode” : “45000”,
“message” : “success”,
“lists_Details” :
 [{
   “List_id” : “”,
   “ShopId” : “”,
   “ ShopName” : “”,
   “Date” : “”,
   “Seen_status” : “”,
   “List_details” : 
[
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
},
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
}]},
{
   “List_id” : “”,
   “ShopId” : “”,
   “ ShopName” : “”,
   “Date” : “”,
   “Seen_status” : “”,
   “List_details” : 
[
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
},
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
}]}]}}

Open in new window

Shruti UpariWeb DeveloperAsked:
Who is Participating?
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
On line  27 you have

$data4['details'] = $data;

Open in new window


Change to

$data4['Item_Response'] = $data;

Open in new window

1
 
Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdCommented:
When you are looping through the data you will have to structure your data there.
0
 
Shruti UpariWeb DeveloperAuthor Commented:
Thank You for your help. I did it as per the instructions given by you all and have finally executed the code in the format which I was getting stuck at. It was all your support that helped me solve this issue.
1
 
Kyle SantosCustomer RelationsCommented:
Hi Shruti,

If you received a solution then you should close your question.  Here are the steps on how to close your question:  http://support.experts-exchange.com/customer/en/portal/articles/2527982-how-do-i-close-my-question-?b_id=44

Let me know if this works or not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.