gpt4 book ai didi

php - 如何从 JSON php 中删除额外的数组

转载 作者:行者123 更新时间:2023-11-29 06:26:03 25 4
gpt4 key购买 nike

我想从我的 json 中删除额外的数组。所以需要帮助。

这就是我当前的 json 的显示方式:

The current json

下图显示了我需要什么(没有数组):

The JSON I want

这是我的 php 代码

<?php 

define('HOST','localhost');
define('USER','appzcvfy_admin');
define('PASS','adminroot1');
define('DB','appzcvfy_sinala_short_stories');


$conn = mysqli_connect(HOST,USER,PASS,DB);

$sql1 = "select * from table_3";

$result1=mysqli_query($conn,$sql1);

$MainArray = array();
$Facilities = array();
$OpeningHours = array();

while ($row1=mysqli_fetch_assoc($result1)) {

$Facilities['t1_id'] = $row1['id'];
$Facilities['t1_name'] = $row1['name'];
$Facilities['t1_link'] = $row1['link'];

$Facilities['OpeningHours'] = array();

$sql2 = "SELECT * FROM table_4 WHERE (id) = ".$row1['id']."";
$result2=mysqli_query($conn,$sql2);

while ($row2=mysqli_fetch_assoc($result2)) {

$OpeningHours['t2_id'] = $row2['id'];
$OpeningHours['t2_name'] = $row2['name'];
$OpeningHours['t2_link'] = $row2['link'];
$OpeningHours['t2_url'] = $row2['url'];

array_push($Facilities['OpeningHours'],$OpeningHours);
}

array_push($MainArray,$Facilities);
}

$jsonData = json_encode(array('server_respnose' => $MainArray), JSON_PRETTY_PRINT);

echo $jsonData;

?>

任何人都可以按照我的意愿重新排列我的 php 代码吗?谢谢

最佳答案

我在发帖之前刚刚看到,我可能已经通过表连接被推送到帖子,但无论如何,这可能有助于简化问题。

<?php

define('HOST','localhost');
define('USER','appzcvfy_admin');
define('PASS','adminroot1');
define('DB','appzcvfy_sinala_short_stories');


$conn = mysqli_connect( HOST, USER, PASS, DB );
$data=array();

$sql='select
t3.`id` as `id1`,
t3.`name` as `name1`,
t3.`link` as `link11,
t4.`id` as `id2`,
t4.`name` as `name2`,
t4.`link` as `link2`,
t4.`url`
from table_3 t3
join table_4 t4 on t3.id=t4.id';

$res=$conn->query( $sql );
if( $res ){
while( $rs=$res->fetch_object() ){
$data[]=array(
't1_id' => $rs->id1,
't1_name' => $rs->name1,
't1_link' => $rs->link1,
'OpeningHours' => array(
't2_id' => $rs->id2,
't2_name' => $rs->name2,
't2_link' => $rs->link2,
't2_url' => $rs->url,
)
);
}
$payload=array('server response'=>$data);
$json=json_encode( $payload );
echo $json;
}
?>

关于php - 如何从 JSON php 中删除额外的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456392/

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