我正在尝试生成一个包含多个json对象的json数组。下面是我输出的代码。
<?php
include_once "/db_connect.php";
$db = new DB_CONNECT();
$head='0';
if(empty($head)){
for($head=1;$head<20;$head++){
if(!empty($head)){
$flatp=mysql_query("SELECT co.id,co.user_id,co.cat_id,co.flatP,hds.HeadName,co.offer_name,u.shop_name,co.cost FROM createdoffers co INNER JOIN categories ct ON ct.id=co.cat_id INNER JOIN
users u ON u.id=co.user_id INNER JOIN heads hds ON hds.id=ct.Head_id
WHERE ct.Head_id='$head' AND ct.id=co.cat_id AND co.user_id=u.id ORDER BY co.flatP DESC LIMIT 1");
if (mysql_num_rows($flatp) >0) {
$response["createdoffers"] = array();
while ($row = mysql_fetch_array($flatp)) {
$recp = array();
$recp["HeadName"]= $row["HeadName"];
$recp["offer_name1"]=$row["offer_name"];
$recp["shop_name1"]=$row['shop_name'];
$recp["cost1"]=$row['cost'];
array_push($response["createdoffers"], $recp);
}
$object=array(array_filter((array)$response));
$res = json_encode($object);
echo trim($res,'[]');
}
}
}
}
?>
这是我的输出,它有不同的json对象和不同的json数组..
{&#34; createdoffers&#34;:[{&#34; HEADNAME&#34;:&#34;汽车摩托车&#34;&#34; offer_name1&#34;:&#34;平坦&#34; ,&#34; shop_name1&#34;:&#34;编码蜘蛛&#34;,&#34; cost1&#34;:&#34; 1300.00&#34;}]}
{&#34; createdoffers&#34;:[{&#34; HeadName&#34;:&#34; Computer and Electroincs&#34;,&#34; offer_name1&#34;:&#34;欢乐时光& #34;,&#34; shop_name1&#34;:&#34;编码蜘蛛&#34;,&#34; cost1&#34;:&#34; 17000.00&#34;}]}
{&#34; createdoffers&#34;:[{&#34; HeadName&#34;:&#34; Food and Dining&#34;,&#34; offer_name1&#34;:&#34; flat&# 34;,&#34; shop_name1&#34;:&#34; codingspider&#34;&#34; COST1&#34;:&#34; 100.00&#34;}]}
我想要的是将所有这三个json对象生成为一个json数组。我无法在这找到什么错误..
答案 0 :(得分:-1)
<?php
include_once "/db_connect.php";
$db = new DB_CONNECT();
$head='0';
$response["createdoffers"] = array();
if(empty($head)){
for($head=1;$head<20;$head++){
if(!empty($head)){
$flatp=mysql_query("SELECT co.id,co.user_id,co.cat_id,co.flatP,hds.HeadName,co.offer_name,u.shop_name,co.cost FROM createdoffers co INNER JOIN categories ct ON ct.id=co.cat_id INNER JOIN
users u ON u.id=co.user_id INNER JOIN heads hds ON hds.id=ct.Head_id
WHERE ct.Head_id='$head' AND ct.id=co.cat_id AND co.user_id=u.id ORDER BY co.flatP DESC LIMIT 1");
if (mysql_num_rows($flatp) >0) {
while ($row = mysql_fetch_array($flatp)) {
$recp = array();
$recp["HeadName"]= $row["HeadName"];
$recp["offer_name1"]=$row["offer_name"];
$recp["shop_name1"]=$row['shop_name'];
$recp["cost1"]=$row['cost'];
array_push($response["createdoffers"], $recp);
}
$object=array(array_filter((array)$response));
$res = json_encode($object);
echo trim($res,'[]');
}
}
}
}
?>