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

时间:2016-02-18 07:50:13

标签: php json array-merge

我有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);
?>

1 个答案:

答案 0 :(得分:0)

您不需要array_merge。只需将两个数组合并到另一个数组中:

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