How can I generate a multi-dimensional array from the SQL query results?

Source: Internet
Author: User
Q: How can I generate a multi-dimensional array of SQL query results? I want to implement this function: I pass a string of numbers for SQL to receive. The number format is the ID value of the dish, similar to "1, 2, 3,", after query, it can be converted into a multi-dimensional array based on the dish category. First, how can I split this string and then use SQL to query and save all the results in a loop? PHPcode $ s $ ids; the received ID string SQLcode $ sqlslt_veg ask: how to generate a multi-dimensional array of SQL query results
I want to implement the following functions:
I pass a string of numbers for SQL reception. The number is the ID value of the dish, similar to "1, 2, 3,". After query, it can be converted into a multi-dimensional array based on the dish category.


First, how can I split this string and then use SQL to query and save all the results in a loop?
PHP code
  $ S = $ ids; // The received ID string

SQL code
  $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);

// This SQL statement queries the detailed information of a dish based on a specific ID of the dish. what if you save all the information of the dish with that ID?

The second problem is how to change the result array of all ID queries to the following multi-dimensional array format.
PHP code
  $ A = array ("2" => Array ("1" => Array ("id" => 728, "name" => "mushroom oil lettuce ", "preprice" => 68, "favprice" => 68, "pic" => jdcpic1341585874.jpg, "num" => 1, "ts" => 0, "fj" => ""), "2" => Array ("id" => 727, "name" => sinhangbang smoked fish, "preprice" => 38, "favprice" => 38, "pic" => jdcpic1320.85755.jpg, "num" => 1, "ts" => 0, "fj" => "")), "1" => Array ("1" => Array ("id" => 749, "name" => Secret: Japanese 20 best abalone, "preprice" => 888, "favprice" => 888, "pic" => jdcpic1356988648.jpg, "num" => 5, "ts" => 0, "fj" => ""), "2" => Array ("id" => 748, "name" => raw juice, slow-boiled, Middle East dried abalone, "preprice" => 788, "favprice" => 788, "pic" => jdcpic1356988600.jpg, "num" => 1, "ts" => 0, "fj" => ""), "3" => Array ("id" => 747, "name" => spiced chicken, "preprice" => 20, "favprice" => 20, "pic" => jdcpic1321388515.jpg, "num" => 1, "ts" => 0, "fj" => "")));

In the code above, 2 and 1 at the periphery represent the category of a dish. This dimension is not fixed and may be either a classification or a classification of four or five,
The second 1, 2, and 3 are the number of the dish, which is automatically generated. The final value is the information of the dish.
How to modify. What if the above SQL statement can generate an array with the same structure as the following multi-dimensional array?
Hope to teach with high fingers, thank you!

------ Solution --------------------
1: $ array_veginfo = $ query_veginfo-> fetch (2, I suspect that 2 is the total number of items. normally, it is designed. 0 should be unlimited.
2:
Echo"
";
print_r($array_veginfo);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.