gpt4 book ai didi

php - Json输出变化

转载 作者:行者123 更新时间:2023-11-30 21:31:08 25 4
gpt4 key购买 nike

我的数据库(mysql)

product_name

ID product_name qty
1 item a 5
2 item b 4
3 item c 3

我的 php 代码

<?php
include("connect.php");
$query="select*from product_name";
$result = mysqli_query($db, $query) or die("Error in Selecting " .mysqli_error($db));
while ($row=mysqli_fetch_assoc($result)){
$arrey[]=$row;
}
echo json_encode($arrey);
?>

输出是

[{
"id": "1",
"productname": "item a",
"qty": "5"
}, {
"id": "2",
"productname": "item b",
"qty": "4"
}, {
"id": "3",
"productname": "item c",
"qty": "3"
}]

我必须面对如下数据

{
"status": "true",
"message": "Data fetched successfully!",
"data": [{
"id": "1",
"productname": "item a",
"qty": "5"
},
{
"id": "2",
"productname": "item b",
"qty": "4"
},
{
"id": "3",
"productname": "item c",
"qty": "3"
}
]
}

怎么做?

最佳答案

您应该将您的数组添加到标题数组中

    $myArray =  ['status' =>"true", 
"message"=> "Data fetched successfully!",
'data' =>$arrey];

.

   <?php
include("connect.php");
$query="select*from product_name";
$result = mysqli_query($db, $query) or die("Error in Selecting " .mysqli_error($db));
while ($row=mysqli_fetch_assoc($result)){
$arrey[]=$row;
}

$myArray = ['status' =>"true",
"message"=> "Data fetched successfully!",
'data' =>$arrey];
echo json_encode($myArray);

?>

关于php - Json输出变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56102605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com