How to build multi-dimensional arrays based on database query results

Source: Internet
Author: User
I want to build a multi-dimensional array based on the database query results. The first result of the database query corresponds to the first layer of nesting, and the second result corresponds to the second layer of nesting, how to do this, declare that I am a Cainiao in advance, please give more instructions & nbsp; query the variable $ sql1 & nbsp; select & nbsp; remark, name & nbsp; from & nbsp; create a multi-dimensional array based on the query results of the arp_careset database
I want to build a multi-dimensional array based on the results of the previous database query. The first result of the database query corresponds to the first layer nesting, and the second result corresponds to the second layer nesting. how can I do this, I am a Cainiao, please give me more instructions

// Query variables

$ Sql1 = "select remark, name from arp_careset where typegroup = 'dissect-position' order by id ";
$ Rs1 = mysql_query ($ sql1 );

While ($ r1 = mysql_fetch_assoc ($ rs1 ))
{Echo $ r1 ['name'];

$ SQL = "select group_concat (name) as name from arp_careset where typegroup <> 'dissect-position' and typegroup like '% ". $ r1 ["remark"]. "% 'Order by id ";
$ Rs = mysql_query ($ SQL );

While ($ r = mysql_fetch_array ($ rs ))
{$ Ar = explode (',', $ r ['name']);
Echo join ('', $ ar );
}}
// Construct an array

$ Mem = array (
Array
(
Lang_show (''),
Array
(
// Start with nesting
Array (
Lang_show ('account _ manager '),
// Layer 2 nesting
Array (
'Group _ list. php, 1 ',
)
// Layer 2 nesting ends
),
// Nesting ends
)
),
);

------ Solution --------------------
Your data organization makes reading inconvenient.
Write in this way
$rs = mysql_query("select name, SUBSTRING_INDEX(typegroup,'-',-1) as secondary, remark from arp_careset where SUBSTRING_INDEX(typegroup,'-',1)='dissect'");
while($r = mysql_fetch_assoc($rs)) {
echo $r['name']. '
';
$rs1 = mysql_query("select name, SUBSTRING_INDEX(typegroup,'-',-1) as secondary, remark from arp_careset where SUBSTRING_INDEX(typegroup,'-',1)='$r[secondary]' and SUBSTRING_INDEX(typegroup,'-',-1) = '$r[remark]'");
while($r = mysql_fetch_assoc($rs1)) {
echo $r['name'] . ' ';
}
echo '
';
}


Body Surface
Nutritional status secretions hair Skin Eye/eyelid limbs/tail external breast genital anus
Lymph node
Armpit lymph node, neck lymph node, groin lymph node, lung lymph node, intestinal lymph node
Head and Neck
Eyelid/Cornea/pupil teeth/ear-nose gums/Whole Brain Skull gland esophagus trachea thyroid
Chest
Aortic arch lung and bronchial
Abdomen
Abdominal mesenteric spleen pancreatic kidney

------ Solution --------------------
Why is data organization poor?
The main group typegroup contains two categories.
The algorithm based on the "adjacent list" should contain only one category information
From the perspective of your data, you must use the typegroup and remark fields to uniquely identify the ownership of a representation.
This is actually an "adjacent list algorithm ".

Although the SUBSTRING_INDEX function can be used to split typegroup to obtain the category information
However, the SUBSTRING_INDEX function is proprietary to mysql. If, for some reason, the database system is changed later, the amount of code modification will be too large.

Although you can submit the splitting work to php, many like operations will reduce the query efficiency.
The program also looks bloated.
------ Solution --------------------
How to create a multi-dimensional array
$ Res = array (); // This is the result array.
$ Rs = mysql_query ("select name, SUBSTRING_INDEX (typegroup, '-',-1) as secondary, remark from arp_careset where SUBSTRING_INDEX (typegroup, '-', 1) = 'dissect '");
While ($ r = mysql_fetch_assoc ($ rs )){
$ Tmp = array (); // initialize a temporary array
$ Rs1 = mysql_query ("select name, SUBSTRING_INDEX (typegroup, '-',-1) as secondary, remark from arp_careset where SUBSTRING_INDEX (typegroup, '-', 1) = '$ r [secondary]' and SUBSTRING_INDEX (typegroup, '-',-1) = '$ r [remark]' ");

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.