Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 数组处理的问题

数组处理的问题

Jun 20, 2016 pm 12:58 PM


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


回复讨论(解决方案)

在封装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
';

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles