Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库创建json文件_Php_Arrays_Json_Mysqli - Fatal编程技术网

Php 从数据库创建json文件

Php 从数据库创建json文件,php,arrays,json,mysqli,Php,Arrays,Json,Mysqli,我需要用以下格式创建这个json { "status": true, "message": "", "orders": [ { "orderId": "1", "orderDate": "1/06/2021", "products": [ {

我需要用以下格式创建这个json

{
  "status": true,
  "message": "",
  "orders": [
    {
      "orderId": "1",
      "orderDate": "1/06/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "5.00"
        },
        {
          "productName": "Product 2",
          "quantity": "2",
          "price": "24.00"
        },
        {
          "productName": "Product 3",
          "quantity": "1",
          "price": "6.50"
        }
      ]
    },
    {
      "orderId": "2",
      "orderDate": "2/06/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "3.00"
        },
        {
          "productName": "Product 2",
          "quantity": "1",
          "price": "11.50"
        }
      ]
    },
    {
      "orderId": "3",
      "orderDate": "03/05/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "3.00"
        },
        {
          "productName": "Product 2",
          "quantity": "1",
          "price": "11.50"
        }
      ]
    }
  ]
}
这是我用来从数据库中检索信息的代码

$stmt = $con->prepare("SELECT OrderID, OrderDate, ProductName, ProductQty, ProductPrice FROM Orders where CustomerID = ?");
$stmt->bind_param("s", $CustomerID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($OrderID, $OrderDate, $ProductName, $ProductQty, $ProductPrice); 
while($stmt->fetch()) { 
    $message[] = array(
        "status" => true, 
        "message" => "", 
        "orders" => array(
            array( 
                "orderId" => "$OrderID", 
                "orderDate" => "$OrderDate", 
                "products" => array(
                    array( 
                        "productName" => "$ProductName",
                        "quantity" => "$ProductQty", 
                        "price" => "$ProductPrice"
                    ), 
                )
            )
        )
    );
}

$json = $message;

header('content-type: application/json');
echo json_encode($json);
这是数据库中信息的显示方式。我不知道如何正确显示有关产品的信息。有人能告诉我如何用php中所需的格式来做吗?提前感谢您的帮助

[
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "620",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "240",
            "quantity": "1",
            "price": "5.00"
          },
          {
            "productName": "270",
            "quantity": "1",
            "price": "10.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "30",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "280",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "610",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  }
]
你可以试试下面的方法

const data = [
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "620",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "240",
                        "quantity": "1",
                        "price": "5.00"
                    },
                    {
                        "productName": "270",
                        "quantity": "1",
                        "price": "10.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "30",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "280",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "610",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": false,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "800",
                        "quantity": "50",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": false,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "1800",
                        "quantity": "50",
                        "price": "5.00"
                    }
                ]
            }
        ]
    }
];

const resutl = data.reduce((acc, cur) => {
    const key = cur.status ? "successItems" : "failureItems";

    if (acc[key]) {
        return {
            ...acc,
            [key]: {
                status: cur.status,
                messgae: '',
                orders: [
                    ...acc[key].orders,
                    ...cur.orders
                ]
            }
        }
    } else {
        return {
            ...acc,
            [key]: {
                status: cur.status,
                messgae: '',
                orders: [
                    ...cur.orders
                ]
            }
        }
    }
}, {});
console.log('resutl', resutl);

console.log('resutl', Object.values(resutl));



你在重复你不应该重复的订单结构。这就是为什么你得到了错误的结构。这是您应该做的(您的代码已修改):

//只指定一次返回响应数组
$message=数组(
“状态”=>正确,
“消息”=>“”,
“订单”=>array();
);
$stmt=$con->prepare(“从CustomerID=?”的订单中选择OrderID、OrderDate、ProductName、ProductQty、ProductPrice”);
$stmt->bind_参数(“s”,$CustomerID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_结果($OrderID、$OrderDate、$ProductName、$ProductQty、$ProductPrice);
而($stmt->fetch()){
//并用您的订单列表填写其“订单”成员
$message[“orders”][]=数组(
“orderId”=>“$orderId”,
“orderDate”=>“$orderDate”,
“产品”=>阵列(
数组(
“产品名称”=>“$productName”,
“数量”=>“$ProductQty”,
“价格”=>“$ProductPrice”
), 
)
);
}
$json=$message;
标题('content-type:application/json');
echo json_encode($json);

感谢您的回复。我应该说我也在使用php。很抱歉。你用的是什么代码?我用的是Javscript。您也可以在php中应用同样的方法。请参阅本文档。看看你是否成功了,否则我会用php发布答案。谢谢。这让我走上了正确的道路。唯一的问题是orderId和orderDate每次都在重复,我只想在我稍微更改了代码后显示它(将$message声明从
bind_params
行和
行之间移动,而
循环到顶部)。我会调试bind_result部分,因为这就是您的问题所在。