如何在 php 中为 json_encode 使用正确的数组合并

how to use proper array merge in php for json_encode

我有 2 个查询,我想将它们全部组合起来,我已经搜索并找到了数组合并,我的问题是如何以正确的方式使用数组合并,我已经尝试了很多次,但总是出错。这是我的代码

<?php
session_start();
include "config.php";

$viewEO;
$viewAcara;

$ideve=mysql_real_escape_string($_GET["id"]);
$mysql = ("SELECT id_eo,nama as namaEO,logo,deskripsi as DeskEO,email,telp from eventorg WHERE id_eo='$ideve'");
$result=mysql_query($mysql);
if (!empty($result))
{       
    while ($row=mysql_fetch_array($result))
    {
        $viewEO[] = array(
        'idEO' => $row['id_eo'],
        'namaeo' => $row['namaEO'],
        'deskripsieo' => $row['DeskEO'],
        'email' => $row['email'],
        'telpon' => $row['telp'],
        'logoeo' => $row['Logo']
        );
    }
}

$mysql2 = ("SELECT id_acara,nama,tanggal,endtanggal,lokasi,imagePath,deskripsi,id_eo from acara WHERE id_eo='$ideve'");
$result2=mysql_query($mysql2);
if (!empty($result2))
{       
    while ($row2=mysql_fetch_array($result2))
    {
        $viewAcara[] = array(
        'idacara' => $row2['id_acara'],
        'namaacara' => $row2['nama'],
        'deskripsi' => $row2['deskripsi'],
        'tanggal' => $row2['tanggal']
        );
    }
}

$final = array_merge(array('array1'=>$viewEO, array('array2'=>$viewAcara)));

mysql_close($con);

header('Content-Type:application/json');
echo json_encode($final);
?>

您不需要 array_merge。只需将两个数组组合成另一个数组即可:

$final = array('array1'=>$viewEO, 'array2'=>$viewAcara);
header('Content-Type: application/json');
echo json_encode($final);