首页 > 解决方案 > 以特定格式从 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

标签: phpsql

解决方案


推荐阅读