Problems with array processing
Such a one-dimensional array, how to get him in order according to the store name, color, size of a multidimensional array?
------to solve the idea----------------------
Write a getall function and a query function in the package 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 a resource variable that connects to a database
}
Initialize an object outside the class, and then write three SQL statements to get three columns, the SQL statement through the GetAll function to get results, the final query results to write their own number of groups put in ... Encapsulation class content too much will not write, do not know whether can help the landlord--
------to solve the idea----------------------
$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 '),
);
$array 2=array ();
foreach ($array as $val)
{
if (array_key_exists ($val [' CKMC '], $array 2))
{
if (array_key_exists ($val [' GG1MC '], $array 2[$val [' CKMC ']])
{
$array 2[$val [' CKMC ']][$val [' GG1MC ']][]= $val;
}
else $array 2[$val [' CKMC ']][$val [' GG1MC ']]=array ($val);
}
else $array 2[$val [' CKMC ']]=array ($val [' GG1MC ']=>array ($val));
}
Echo '';
Print_r ($array 2);
Echo '
';