gpt4 book ai didi

php - 为第一个 JSON PHP 添加值

转载 作者:行者123 更新时间:2023-11-29 10:32:31 25 4
gpt4 key购买 nike

我有一些 php 可以像这样将结果显示为 JSON

$dp = $_GET['dp'];

$query = mysql_query("select trip_id, trip_desc from current_data where dispatcher='".$dp."'");

$json = array();

while($row = mysql_fetch_assoc($query)){

$tripdata["code"]=$row["trip_id"]." - ".$row["trip_code"];

$json[] = $tripdata;

}

echo json_encode($json);

结果是这样的

[{"code":"S1.001 - UK"},{"code":"S1.002 - US"},{"code":"S1.003 - CA"}]

如何在 JSON 的第一个结果中添加值,我想要这样的结果?

 [**{"code":"Select Country"}**,{"code":"S1.001 - UK"},{"code":"S1.002 - US"},{"code":"S1.003 - CA"}]

谢谢

最佳答案

在 while 循环之前将第一个值插入数组:

$json[] = [
'code' => 'Select Country',
];

while($row = mysql_fetch_assoc($query)){

$tripdata["code"]=$row["trip_id"]." - ".$row["trip_code"];

$json[] = $tripdata;

}

echo json_encode($json);

这将在 json 的开头添加所需的值。

注意:

mysql_* 起已弃用并从 起删除。因此,请改用 mysqli_*PDO
Why shouldn't I use mysql_* functions in PHP?

关于php - 为第一个 JSON PHP 添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47138104/

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