如何在数组中将数组作为最后一个元素附加到PHP中的键? 我需要以下输出。
我目前的代码是:
$qr=mysql_query("SELECT * from category") or die(mysql_error());
$rows = array();
while($r= mysql_fetch_assoc($qr))
{
$qr2=mysql_query("SELECT subcatname from subcategory where catname='".$r['catname']."'") or die(mysql_error());
$rows2 = array();
$rows['response'][] = $r;
while($r2 = mysql_fetch_assoc($qr2))
{
$rows2[]=$r2;
// $rows['subcatname'] = $r2;
}
$rows['response'][]= $rows2;
}
echo (json_encode($rows));
我得到这样的输出..
{"response":[{"id":"1","catname":"Fruits & Vegetables","cattagline":"Potato, Tomato, Apples","catimg":"qwrefrweafwsfg"},[{"subcatname":"FRUITS"},{"subcatname":"VEGETABLES"},{"subcatname":"FROZEN"}],{"id":"4","catname":"Dairy","cattagline":"dsgfasgtersa","catimg":"sfsdafgasg"},[{"subcatname":"Milk & CURD"}]]}
我需要使用catname,cattagline,...插入子命名。 如下所示:
{"response":[{"id":"1","catname":"Fruits & Vegetables","cattagline":"Potato, Tomato, Apples","catimg":"qwrefrweafwsfg","subcatname":{"0":"Fruits","1":"VEGETABLES"}},{"id":"4","catname":"Dairy","cattagline":"dsgfasgtersa","catimg":"sfsdafgasg","subcatname":{"0":"Milk & CURD"}}]}