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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 SantosQuality AssuranceCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.