从多个PHP数组创建JSON

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从多个PHP数组创建JSON相关的知识,希望对你有一定的参考价值。

我在php文件中创建了三个数组,所有这些数组都来自mysql数据库。即使我使用json_encode方法来准备数组,结果也不是可解析的JSON。

我是JSON和PHP的新手,但我尝试将三个数组编码为一个变量,以及三个不同的变量。但是在我回显结果的两种情况下,它都是无效的JSON。

我在PHP文件的开头创建了数组。

$physicianArr = array();

$patientArr = array();

$apmtArr = array();

我使用这个通用方法来填充所有数组(行用于打破三个示例,不包含在代码中。)

if (mysqli_num_rows($resultPhysician) > 0) {
  while($row=mysqli_fetch_assoc($resultPhysician)) {
    $physicianArr[] = $row;
  }
  $physicianJSON = json_encode($physicianArr);
}

if (mysqli_num_rows($resultPatient) > 0) {
  while($row=mysqli_fetch_assoc($resultPatient)) {
    $patientArr[] = $row;
  }
  $patientJSON = json_encode($patientArr);
}

if (mysqli_num_rows($resultApmt) > 0) {
  while($row=mysqli_fetch_assoc($resultApmt)) {
    $apmtArr[] = $row;
  }
  $apmtJSON = json_encode($apmtArr);
}

结果:

当我输入url来在Web浏览器上检索此PHP文件时,结果就是三个独立的数组,而不是一个JSON:

[
    {"UserID":"3","FirstName":"Jane","LastName":"Parkey"}, 
    {"UserID":"4","FirstName":"Jamie","LastName":"Crane"},
    {"UserID":"5","FirstName":"Jerry","LastName":"Martin"},
    {"UserID":"6","FirstName":"Alexander","LastName":"Dollar"},
    {"UserID":"8","FirstName":"Bob","LastName":"Loblaw"},
    {"UserID":"11","FirstName":"Mary","LastName":"Robbins"},
    {"UserID":"15","FirstName":"testy","LastName":"testy"}
][
    {"UserID":"1","FirstName":"Joe","LastName":"Smith"},
    {"UserID":"2","FirstName":"Adam","LastName":"Stone"},
    {"UserID":"9","FirstName":"Michael","LastName":"Jordan"},
    {"UserID":"10","FirstName":"Tom","LastName":"Holland"},
    {"UserID":"12","FirstName":"test1","LastName":"test1"},
    {"UserID":"19","FirstName":"Will","LastName":"Smith"},
    {"UserID":"20","FirstName":"Joe","LastName":"Imburgia"}
][
    {"apmtID":"1","PhysicianID":"15","apmtDate":"Jun 26, 2019","apmtTime":"09:00:00 AM","PatientID":"1"},
    {"apmtID":"2","PhysicianID":"15","apmtDate":"test","apmtTime":"test","PatientID":"1"},
    {"apmtID":"4","PhysicianID":"15","apmtDate":"Apr 20, 2019","apmtTime":"7:10:36 AM","PatientID":"1"}
]
答案

这是一个建议。如果你想要一个数据结构,那么只回显一个。

// make sure all arrays exist
$physicianArr   = [];
$patientArr     = [];
$apmtArr        = [];

if (mysqli_num_rows($resultPhysician) > 0) {
    while($row=mysqli_fetch_assoc($resultPhysician)) {
        $physicianArr[] = $row;
    }
}   

if (mysqli_num_rows($resultPatient) > 0) {
    while($row=mysqli_fetch_assoc($resultPatient)) {
        $patientArr[] = $row;
    }
}

if (mysqli_num_rows($resultApmt) > 0) {
    while($row=mysqli_fetch_assoc($resultApmt)) {
        $apmtArr[] = $row;
    }
}

echo json_encode( 
                [
                    'Physicians'    => $physicianArr
                    'Patients'      => $patientArr
                    'Ampt'          => $apmtArr
                ] 
            );
另一答案

您可以使用函数array_merge,以便将数组合并为一个数组。一旦你这样做,你可以使用json_encode函数来编码这个包含三个数组中所有数据的数组。

以上是关于从多个PHP数组创建JSON的主要内容,如果未能解决你的问题,请参考以下文章

从 PHP 中的循环中排除(黑名单)多个 Json 条目

创建PHP动态多字段的JSON格式

PHP 如何从 AJAX 调用中发布多个数组/json 值并在同一个 SQL 查询中运行它们?

如何从 3 个 php 数组创建 json 文件

有没有办法使用相同的布局动态创建片段并向它们显示数据?

使用任何PHP循环从数据库表创建多个html表