dsv38843 2015-05-03 16:05
浏览 354
已采纳

从mysql输出数据到数组json让我发疯

im trying to pull out a menu in this json format : check the output : http://www.alacarta.do/iphone/webservices/restaurants_menu2.php?r=415

The thing is that iterating of the Category plates, it duplicates the plates and then add the corresponding correct ones in each Category. all the time. check the output in the link. first category is TO SHARE. and the plates are ok, but the second category FRIES BAR, will throw again the plates from TO SHARE and then the correct plates in its category

<?

$where = empty($_GET['r'])? NULL : 'id = '. intval($_GET['r']); 
$restaurant = $cmp->empresas($where,"nombre ASC")->fetch();
$json = array();
$arraynombre = array();

        while($orden = $cmp->platos_tipos_orden("id_empresa = {$restaurant->id}","orden ASC")->foreachrow()):

        $tipo = $cmp->platos_tipos("id = {$orden->id_tipo}")->fetch();

            while($menu = $cmp->platos_menu("id_tipo = {$orden->id_tipo} AND id_empresa = {$orden->id_empresa}")->foreachrow()):            
            $p = $cmp->platos_lista("id = {$menu->id_plato}")->fetch();
            $pnombre = $p->nombre; 
            $pid = $p->id;
            $pprecio = $p->precio;
            $arraynombre1 = array('plato_id'=>$pid,'plato_nombre'=>$pnombre,'precio'=>$pprecio);

                if (in_array($arraynombre1['plato_id'], $arraynombre['plato_id'])) continue;

            $arraynombre[] = $arraynombre1;

endwhile;


$jsondata = array('tipo'=> utf8_decode($tipo->nombre),'platos' => $arraynombre);
$json[] = $jsondata;             
endwhile;


      echo json_encode( array("menu"=>$json));

?>
  • 写回答

1条回答 默认 最新

  • dougang2749 2015-05-03 16:19
    关注

    Your issue is probably this line -

    $arraynombre[] = $arraynombre1;
    

    as you continually add to the array, not overwrite the previous loops values.

    Try adding a key to it that is unique to each loop, ie. $orden->id_tipo -

    $arraynombre[$orden->id_tipo][] = $arraynombre1;
    

    Then you would also need to change

    $jsondata = array('tipo'=> utf8_decode($tipo->nombre),'platos' => $arraynombre);
    

    to

    $jsondata = array('tipo'=> utf8_decode($tipo->nombre),'platos' => $arraynombre[$orden->id_tipo]);
    

    note- it is hard to follow all your code logic, so [$orden->id_tipo] might need to be [$orden->id_empresa] or could be a similar counter var [$x] that you increase on each loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器