首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json输出更改

Json输出更改
EN

Stack Overflow用户
提问于 2019-05-13 03:11:28
回答 1查看 35关注 0票数 1

我的数据库(Mysql)

代码语言:javascript
复制
product_name

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

我的php代码

代码语言:javascript
复制
<?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);
?>

输出为

代码语言:javascript
复制
[{
    "id": "1",
    "productname": "item a",
    "qty": "5"
}, {
    "id": "2",
    "productname": "item b",
    "qty": "4"
}, {
    "id": "3",
    "productname": "item c",
    "qty": "3"
}]

我不得不像贝罗一样面对数据。

代码语言:javascript
复制
{
    "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"
        }
    ]
}

该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-13 03:17:26

您应该将数组添加到头数组中

代码语言:javascript
复制
    $myArray =  ['status' =>"true", 
                    "message"=> "Data fetched successfully!", 
                    'data' =>$arrey];

代码语言:javascript
复制
   <?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);

  ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56102605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档