0

How can I format the code below to create a json array separated by commas in a php while loop? The json returned cannot be decoded again since it seems invalid

<?php //database config
$rtv = mysqli_query($link, "SELECT * FROM ".$datatable." WHERE productid='".$id."' ");
while($bld =mysqli_fetch_assoc($rtv)){
    $date = $bld["timestamp"];                                     
    $data = [];
    $data[] =[
        "requestId" => $requestid,
        "Service" => $service,
        "Date" => $date
    ];
    echo json_encode($data, JSON_PRETTY_PRINT);

What I get is


     [
        {
        "Id": "90223-1475789-2",
        "Service": "Maintenance",
        "Date": "2021-06-30 23:07:15"
       }
    ][
      {
        "Id": "90223-1475789-2",
        "Service": "Maintenance",
        "Date": "2021-06-30 23:07:15"
       }
     ]

instead of desired format which is


     [
       {
        "Id": "90223-1475789-2",
        "Service": "Maintenance",
        "Date": "2021-06-30 23:07:15"
       },
       {
        "Id": "90223-1475789-2",
        "Service": "Maintenance",
        "Date": "2021-06-30 23:07:15"
       }
     ]
Don't Panic
  • 39,820
  • 10
  • 58
  • 75
Stevooh
  • 23
  • 5
  • 2
    Remove the `[]` from `array([...])` when you assign it to data. In addition to that, why are you getting two records? Do you assign arrays to `data` in a loop? – Vasilis G. Jul 06 '21 at 20:37
  • If in a loop, you can [push](https://www.php.net/manual/en/function.array-push.php) records onto the array: `$data[] = ["Id" => $requestid, "Service" => $service, "Date" => $date]` – showdev Jul 06 '21 at 20:38
  • @showdev This doesnt make any difference to what I have. The array should be separated by commas like `[{ "id" : "1"}, { "id" : "2"},.....]`. – Stevooh Jul 06 '21 at 20:47
  • @VasilisG. This is an extract of the code am writing, I have deleted some parts as they are not too much important in the solution. "Do you assign arrays to data in a loop?" What does that mean? – Stevooh Jul 06 '21 at 20:52
  • [Example here](https://onecompiler.com/php/3x4pfxdyh). Some parts you deleted may be important to the question, e.g. if there is a looping structure you haven't shown. You referenced a `while` loop. Can we see it? – showdev Jul 06 '21 at 20:53
  • ` $requestid, "Service" => $service, "Date" => $date ]; echo json_encode($data, JSON_PRETTY_PRINT); ?>` @showdev this is the other part – Stevooh Jul 06 '21 at 21:07
  • 1
    It looks like you define and output `$data` inside the loop. This means that every loop with overwrite the previous `$data` assignment. Define it once before the loop output it after the loop. – showdev Jul 06 '21 at 21:09

3 Answers3

1

It seems that you may be redefining and outputting $data upon each loop iteration. But what it seems you want to do is append data to the array upon each loop iteration and then output the entire array after the loop.

This is what I think you're doing:

while ( ... ) {

    $data =array([
        "Id" => $requestid,
        "Service" => $service,
        "Date" => $date
    ]);
    
    echo json_encode($data, JSON_PRETTY_PRINT);

}

And what I recommend instead:

$data = [];

while ( ... ) {

    $data[] = [
        "Id" => $requestid,
        "Service" => $service,
        "Date" => $date
    ];
    
}

echo json_encode($data, JSON_PRETTY_PRINT);
showdev
  • 27,301
  • 36
  • 51
  • 71
1

To get the output you want you need to create a single array and push the data onto it before using json_encode()

<?php
// Create an element
// Some dummy data

$requestid = "requestid";
$service = "service";
$date = "today";

$data =[
             "Id" => $requestid,
             "Service" => $service,
             "Date" => $date
        ];

// Create a parent array
$json = [];
// Add the element to the array twice
$json[] = $data;
$json[] = $data;

echo json_encode($json, JSON_PRETTY_PRINT);

Output:

[
    {
        "Id": "requestid",
        "Service": "service",
        "Date": "today"
    },
    {
        "Id": "requestid",
        "Service": "service",
        "Date": "today"
    }
]

Demo:https://3v4l.org/FlPuc

0

You code returns multi dimensional arrays instead of one array, so you need to merge the array before you encode it Try this merging the arrays using the array_merge() function // I assume these comes from a db query

$requestid =2;
$service ="cleaning";
$date ="Monday";



$requestid1 =3;
$service1 ="washing";
$date1 ="tuesday";


$data =array([
         "Id" => $requestid,
         "Service" => $service,
         "Date" => $date
    ],[
         "Id" => $requestid1,
         "Service" => $service1,
         "Date" => $date1
    ]);
    
    
    header("Content-Type: application/json; charset=UTF-8");
         echo json_encode(array_merge($data),true);

See the results here when you ran it