I am doing 2 queries on the same MySQL table.
Full code here
$json = array();
$json['soldArticles'] = array();
for ($brojac = 0; $brojac < count($storeLocationID); $brojac ) {
$storeName = storeFullName($storeLocationID[$brojac]);
$sqlTOTAL = "SELECT SUM(art_ukupno) AS ukupno, art_nazivProdavnice FROM tbl_ProdatiArtikli WHERE art_datum BETWEEN '2022-08-10' AND '2022-08-12'
AND art_nazivProdavnice = '$storeName'";
$result = $conn->query($sqlTOTAL);
$sql = "SELECT art_kategorija, SUM(art_kolicina) AS Qty, SUM(art_ukupno) AS Total
FROM tbl_ProdatiArtikli
WHERE art_datum BETWEEN '2022-08-10' AND '2022-08-12' AND art_nazivProdavnice = '$storeName'
GROUP BY art_kategorija, art_nazivProdavnice
ORDER BY art_nazivProdavnice ASC, art_kategorija ASC";
$result1 = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$json["Prodavnica"] = $row["art_nazivProdavnice"];
$json["Total"] = $row["ukupno"];
while ($row1 = mysqli_fetch_array($result1)) {
$index['Category'] = $row1['art_kategorija'];
$index['Qty'] = $row1['Qty'];
$index['Total'] = $row1['Total'];
array_push($json['soldArticles'], $index);
}
}
echo json_encode($json, JSON_UNESCAPED_UNICODE);
}
This is the output that I get
{
"soldArticles":[
{
"Category":"16",
"Qty":"10.90",
"Total":"4895.00"
},
{
"Category":"19",
"Qty":"27.00",
"Total":"275.00"
},
...
],
"Prodavnica":"Store 01",
"Total":"162640.00"
}
Now I cannot seem to add another (PARENT) array that will have the name ex. DATA, and within that array will be the output of all store objects like below.
Does anyone have any ideas on how to fix this and add the main array to wrap the existing objects?
Example of what I want to achieve
{
"DATA":[
{
"soldArticles":[
{
"Category":"16",
"Qty":"10.90",
"Total":"4895.00"
},
{
"Category":"19",
"Qty":"27.00",
"Total":"275.00"
},
...
],
"Prodavnica":"Store 01",
"Total":"162640.00"
},
//next store object,
//next store object
]
CodePudding user response:
Whenever you are trying to push an array into another array you can use following methods
$data = array(); #this will initialize an empty array
// method 1
$data[] = $json;
// method 2
array_push($data, $json);