首頁  >  文章  >  後端開發  >  请问:怎么将sql查询结果生成多维数组

请问:怎么将sql查询结果生成多维数组

WBOY
WBOY原創
2016-06-13 13:07:331050瀏覽

请教:如何将sql查询结果生成多维数组
我想实现的功能是这样的:
我传递一串数字供sql接收,数字的形式是菜品的ID值,类似“1,2,3,”,经过查询可以变成根据菜品分类而来的多维数组。


第一个问题,我如何将这个字符串分离开后循环利用sql查询并保存全部结果呢?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->$s=$ids;//接收到的ID字符串

SQL code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$sqlslt_veginfo="select {$tb_veg}.veg_id,{$tb_veg}.vsort_id,{$tb_veg}.veg_name,{$tb_veg}.veg_preprice,{$tb_veg}.veg_favprice,{$tb_veg}.veg_picstr,{$tb_veg}.veg_ptfs,{$tb_veg}.veg_ts,{$tb_veg}.veg_fj from {$tb_veg} 
where {$tb_veg}.veg_id='{$s}'and {$tb_veg}.vdel_vfy='0'";
$query_veginfo=$pdodb->query($sqlslt_veginfo);
$array_veginfo=$query_veginfo->fetch(2);


//这段sql是根据菜品的某个ID去查询菜品的详细信息,如果保存那一串ID的全部菜品信息呢?

第二个问题,保存了全部ID查询后结果的数组,如何变为下面的多维数组形式
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$a=array(
    "2" => Array
        (
            "1" => Array
                (
                    "id" => 728,
                    "name" => "玉带香油莴笋",
                    "preprice" => 68,
                    "favprice" => 68,
                    "pic" => jdcpic1342685874.jpg,
                    "num" => 1,
                    "ts" => 0,
                    "fj" => ""
                ),
            "2" => Array
                (
                    "id" => 727,
                    "name" => 新杭帮熏鱼,
                    "preprice" => 38,
                    "favprice" => 38,
                    "pic" => jdcpic1342685755.jpg,
                    "num" => 1,
                    "ts" => 0,
                    "fj" => ""
                )

        ),
    "1" => Array
        (
            "1" => Array
                (
                    "id" => 749,
                    "name" => 秘制日本二十头极品鲍鱼,
                    "preprice" => 888,
                    "favprice" => 888,
                    "pic" => jdcpic1342688648.jpg,
                    "num" => 5,
                    "ts" => 0,
                    "fj" => ""
                ),
            "2" => Array
                (
                    "id" => 748,
                    "name" => 原汁慢煮焖煨中东干鲍鱼,
                    "preprice" => 788,
                    "favprice" => 788,
                    "pic" => jdcpic1342688600.jpg,
                    "num" => 1,
                    "ts" => 0,
                    "fj" => ""
                ),
            "3" => Array
                (
                    "id" => 747,
                    "name" => 炝椒酱爆嫩鸡丁,
                    "preprice" => 20,
                    "favprice" => 20,
                    "pic" => jdcpic1342688515.jpg,
                    "num" => 1,
                    "ts" => 0,
                    "fj" => ""
                )
        )
);


以上代码里,最外围的2和1 代表的是菜品所属的分类,该维数不是固定的,可能都是一个分类,也可能是四五个分类中的,
其次的1,2,3就是菜的编号了,自动来的,最后的值是菜品的信息。
如何修改。利用上面的sql最后可以生成结构与下面多维数组相同的数组呢?
还望高手指教,谢谢!

------解决方案--------------------
1:$array_veginfo=$query_veginfo->fetch(2);你把这个 2改为0或是10000看得到的是什么,最好能看看如何实现的,我怀疑2是总条数,正常情况下设计,0应是不限制
2:
echo "
";<br>print_r($array_veginfo); <div class="clear">
                 
              
              
        
            </div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn