gpt4 book ai didi

php - JSON数据集结构

转载 作者:行者123 更新时间:2023-11-30 22:15:07 24 4
gpt4 key购买 nike

我正在将 mysql 记录创建到 json 数组中,但 json 数据集似乎存在一些问题。 PFB 代码:-

JSON 输出来了:-

[{
"ID": "1",
"Coupon_ID": "ABC1",
"Campaign_ID": "353",
"Campaign_Name": "Test1",
"Title": "Test1",
"Description": "Test1",
"Category": "Test1",
"Type": "Coupon",
"Type_Value": "1231",
"Tracking_URL": "http://abc1.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}][
{
"ID": "2",
"Coupon_ID": "ABC2",
"Campaign_ID": "3532",
"Campaign_Name": "Test2",
"Title": "Test2",
"Description": "Test2",
"Category": "Test2",
"Type": "Coupon",
"Type_Value": "1232",
"Tracking_URL": "http://abc2.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}]

我想要这样的东西:-

 [{
"ID": "1",
"Coupon_ID": "ABC1",
"Campaign_ID": "353",
"Campaign_Name": "Test1",
"Title": "Test1",
"Description": "Test1",
"Category": "Test1",
"Type": "Coupon",
"Type_Value": "1231",
"Tracking_URL": "http://abc1.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}
{
"ID": "2",
"Coupon_ID": "ABC2",
"Campaign_ID": "3532",
"Campaign_Name": "Test2",
"Title": "Test2",
"Description": "Test2",
"Category": "Test2",
"Type": "Coupon",
"Type_Value": "1232",
"Tracking_URL": "http://abc2.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}]

我的意思是 [] 这些括号而不是针对每条记录出现,这些应该只出现一次。

PHP 代码:-

<?php
header('Content-type: application/json');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "couponsdeals";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$apikey = $_GET['API_KEY'];
$sql = "SELECT AFF_ID FROM api WHERE API_KEY= '$apikey' ";

//$sql1= "SELECT ID,Coupon_ID,Campaign_ID,Campaign_Name,Title,Description,Category,Type,Type_Value,Tracking_URL,Added_Date,Expiry_Date FROM couponcode_table ";

$sql1 = "SELECT * FROM couponcode_table ";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$data = $row["AFF_ID"];
if ($result1) {
while ($row1 = $result1->fetch_assoc()) {
$data1 = $row1["Tracking_URL"];
$mydata = explode("&", $data1);
if (isset($mydata[2])) {
$json1 = array();
$mydata2 = $mydata[0] . "&aff_id=" . $data . "&" . $mydata[2];
$values = array(
'ID' => $row1['ID'],
'Coupon_ID' => $row1['Coupon_ID'],
'Campaign_ID' => $row1['Campaign_ID'],
'Campaign_Name' => $row1['Campaign_Name'],
'Title' => $row1['Title'],
'Description' => $row1['Description'],
'Category' => $row1['Category'],
'Type' => $row1['Type'],
'Type_Value' => $row1['Type_Value'],
'Tracking_URL' => $mydata2,
'Added_Date' => $row1['Added_Date'],
'Expiry_Date' => $row1['Expiry_Date']

);
array_push($json1, $values);
echo json_encode($json1, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} else {
$json = array();
$mydata1 = $mydata[0] . "&aff_id=" . $data;
$value = array(
'ID' => $row1['ID'],
'Coupon_ID' => $row1['Coupon_ID'],
'Campaign_ID' => $row1['Campaign_ID'],
'Campaign_Name' => $row1['Campaign_Name'],
'Title' => $row1['Title'],
'Description' => $row1['Description'],
'Category' => $row1['Category'],
'Type' => $row1['Type'],
'Type_Value' => $row1['Type_Value'],
'Tracking_URL' => $mydata1,
'Added_Date' => $row1['Added_Date'],
'Expiry_Date' => $row1['Expiry_Date']
);
array_push($json, $value);
echo json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
}
}
}
} elseif (!$result) {
echo "0 results";
}
$conn->close();
?>

最佳答案

您需要在 while 循环之外初始化/回显您的 JSON 数组:

$json = array();
while(....) {
while(....) {
//.......
array_push($json, $value);
//.......
}
}
echo json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

关于php - JSON数据集结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38589876/

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