php - 以特定格式从 php 输出 json
问题描述
我需要以 json 格式从数据库输出数据,虽然我在 StackOverflow 的帮助下设法让它工作,但我需要输出的另一个数据出现了,我需要重写我的整个 php 文件。目前,我有这个 php 文件:
<?php
class SeznamMagnetekApi extends BaseApi
{
public function ToProcess($parametry)
{
$magnetkyInfo = Database::SqlGetFetchAll("select monumentid, name, region, obec, okres, content, web from monuments");
$i = 0;
foreach ($magnetkyInfo as $info)
{
$i++;
$obj = ["monumentid" => $info["monumentid"], "name" => $info["name"], "region" => $info["region"], "obec" => $info["obec"], "okres" => $info["okres"], "content" => $info["content"], "web" => $info["web"]];
$this->json['monumentid' . $i] = $obj;
}
}
}
?>
但我需要添加此选择:“select shop, hours from shopplace where Monumentname = ?;”, array($id)。
我怎样才能保持我的 json 格式不变?我设法得到的只是这种格式,但它对我来说无法使用:
<?php
class SeznamMagnetekApi extends BaseApi
{
public function ToProcess($parametry)
{
$magnetkyInfo = Database::SqlGetFetchAll("select monumentid, name, region, obec, okres, content, id, web from monuments");
$i = 0;
foreach ($magnetkyInfo as $info)
{
$i++;
$id = $info['id'];
$obj = ["monumentid" => $info["monumentid"], "name" => $info["name"], "region" => $info["region"], "obec" => $info["obec"], "okres" => $info["okres"], "content" => $info["content"], "web" => $info["web"]];
$this->json['monumentid' . $i] = $obj;
$xxx = Database::SqlGetFetchAll("select shop, hours from shopplace where monumentname = ?;", array($id));
$this->json['prodejni_mista_pro' . $i] = $xxx;
}
}
}
?>
有 PHP 专业人士可以帮助我吗?:D
解决方案
推荐阅读
- selenium - 如何在硒中使用爬虫代理
- ios - SwiftUI 列表和延迟加载
- python - 未找到带有参数 '('',)' 的 'detail_account' 的反向操作。尝试了 1 种模式:['account/account/(?P
[^/]+)$'] - jquery - 为什么 CORS 策略阻止我从同一文件夹加载 HTML 片段?
- quarkus - 从 quarkus graphQL 访问 RestClient 会出现 RESTEASY004655:无法调用请求:javax.net.ssl.SSLHandshakeException 错误
- wordpress - 使用 SSH 连接到服务器上的 Git
- matlab - 多边形边界上彼此距离相等的点(坐标) - Matlab
- python - 使用 Python 脚本的 Google Cloud Functions 中的未知错误
- c - 在 WDM 中,即使分配的内存被释放,对 ExAllocatePoolWithTag 和 ExFreePoolWithTag 使用不同的标签是否会导致内存泄漏?
- java - 将 swagger-core 2.1.3 与 JDK httpserver 一起使用