Array processing problems

Source: Internet
Author: User
Array processing is such a one-dimensional array, how to turn it into a multi-dimensional array according to shop name, color, size specifications in turn?


Reply to discussion (solution)

Write a getAll function and a query function in the encapsulated MySQL class.
Public function getAll ($ SQL)
{
$ Rs = $ this-> query ($ SQL );
$ List = array ();
If (! $ Rs)
{
Return false;
}
Else {
While ($ row = mysql_fetch_assoc ($ rs ))
{
$ List [] = $ row;
}
Return $ list;
}
}

Public function query ($ SQL)
{
Return mysql_query ($ SQL, $ this-> conn); // $ this-> conn is the resource variable used to connect to the database.
}
Initialize an object outside the class, write three SQL statements to get three columns, and send the SQL statement using the getAll function to obtain the result, finally, you can write an array into the query result... I don't want to write too much encapsulated class content. I don't know if I can help you --

$array=array(array('CKMC'=>'store1','GG1MC'=>'color1','GG2MC'=>'size1'),array('CKMC'=>'store1','GG1MC'=>'color1','GG2MC'=>'size2'),array('CKMC'=>'store1','GG1MC'=>'color2','GG2MC'=>'size1'),array('CKMC'=>'store1','GG1MC'=>'color2','GG2MC'=>'size2'),array('CKMC'=>'store2','GG1MC'=>'color1','GG2MC'=>'size1'),array('CKMC'=>'store2','GG1MC'=>'color2','GG2MC'=>'size2'),);$array2=array();foreach($array as $val){if(array_key_exists($val['CKMC'],$array2)){if(array_key_exists($val['GG1MC'],$array2[$val['CKMC']])){$array2[$val['CKMC']][$val['GG1MC']][]=$val;}else $array2[$val['CKMC']][$val['GG1MC']]=array($val);}else $array2[$val['CKMC']]=array($val['GG1MC']=>array($val));}echo '
';print_r($array2);echo '
';

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.