Home > Backend Development > PHP Tutorial > 数组处理的问题

数组处理的问题

WBOY
Release: 2016-06-20 12:58:10
Original
862 people have browsed it

这样一个一维数组,怎么把他弄成依次按照店铺名、颜色、尺码规格的一个多维数组?


回复讨论(解决方案)

在封装MySQL类里面写个getAll函数 和query函数
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是连接数据库的资源变量
}
在类的外面初始化一个对象,然后写三个sql语句获取三个列,把sql语句通过getAll函数发出去获得结果,最后查询结果自己再写个数组放进去。。。封装类的内容太多就不写了,不知道能不能帮到楼主- -

$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 '<pre class="brush:php;toolbar:false">';print_r($array2);echo '
Copy after login
';

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template