Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP/MySQL生成多维JSON数组_Php_Json - Fatal编程技术网

从PHP/MySQL生成多维JSON数组

从PHP/MySQL生成多维JSON数组,php,json,Php,Json,我想创建一个JSON对象,如下例所示 第一部分可以使用MySQL\u fetch\u assoc和array\u push从MySQL数据库生成: while ($row = mysql_fetch_assoc($result)) { array_push($returnArray, $row); } 第二部分将附加在程序中的程序末尾。 我在PHP中操作数组时遇到问题,无法实现我想要的功能 $returnArray['DateUpdated'] = '20091209'; $re

我想创建一个JSON对象,如下例所示


第一部分可以使用MySQL\u fetch\u assoc和array\u push从MySQL数据库生成:

while ($row = mysql_fetch_assoc($result)) 
{ 
  array_push($returnArray,  $row); 
}
第二部分将附加在程序中的程序末尾。 我在PHP中操作数组时遇到问题,无法实现我想要的功能

$returnArray['DateUpdated'] = '20091209';
$returnArray['UpdatUser'] = 'Bob';
然后对返回数组进行json_编码,应该设置


然后对返回数组进行json_编码,您就可以进行设置了。

这样就可以了

$productArray = array();
while ($row = mysql_fetch_assoc($result)) 
{ 
  array_push($productArray,  $row); 
}

$returnArray['Products'] = $productArray;
$returnArray['DateUpdated'] = $dateUpdated; // 20091209 in your example
$returnArray['UpdatUser'] = $updatUser; // Bob in your example

$jsonEncoded = json_encode($returnArray);

更多信息和

这应该可以解决问题

$productArray = array();
while ($row = mysql_fetch_assoc($result)) 
{ 
  array_push($productArray,  $row); 
}

$returnArray['Products'] = $productArray;
$returnArray['DateUpdated'] = $dateUpdated; // 20091209 in your example
$returnArray['UpdatUser'] = $updatUser; // Bob in your example

$jsonEncoded = json_encode($returnArray);
更多信息,请尝试:

$array = array (
    'Products' => array (),
    'DateUpdated' => '20091209',
    'UpdateUser' => 'Bob',
);

while ($row = mysql_fetch_assoc($result))
    $array['Products'][] = $row;

$json = json_encode($array);
尝试: