我如何在数组中追加数组作为最后一个元素与键在PHP我需要在输出下面..?

时间:2015-10-31 11:02:38

标签: php arrays json multidimensional-array arraylist

如何在数组中将数组作为最后一个元素附加到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"}}]}

1 个答案:

答案 0 :(得分:1)

尝试array_ push。 这是Documentation

的链接