Home >Backend Development >PHP Tutorial >重组数组,实现类似淘宝一样的合并订单

重组数组,实现类似淘宝一样的合并订单

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:13:31920browse

<code>array (
  '0' =>
    array (
      'id' => string '22',
      'title' => string '商品名称',
      'test' => string '123',
      )
  '1' =>
    array (
      'id' => string '23',
      'title' => string '商品名称',
      'test' => string '125',
      )
  '2' =>
    array (
      'id' => string '30',
      'title' => string '商品名称',
      'test' => string '123',
      )
)</code>

类似上面的数组,需要把test字段相同的组合在一起,并在前台输出,类似电商网站的订单合并,在线等

回复内容:

<code>array (
  '0' =>
    array (
      'id' => string '22',
      'title' => string '商品名称',
      'test' => string '123',
      )
  '1' =>
    array (
      'id' => string '23',
      'title' => string '商品名称',
      'test' => string '125',
      )
  '2' =>
    array (
      'id' => string '30',
      'title' => string '商品名称',
      'test' => string '123',
      )
)</code>

类似上面的数组,需要把test字段相同的组合在一起,并在前台输出,类似电商网站的订单合并,在线等

其实很简单:

<code>$arra =   array (
            '0' => array ('id' =>  '22', 'title' =>  '商品名称', 'test' =>  '123',),
  '1' => array ('id' =>  '23', 'title' =>  '商品名称', 'test' => '125',),
  '2' => array ('id' =>  '30', 'title' =>  '商品名称', 'test' =>  '123',));
        $result = array();
        foreach($arra as $key => $value){
            $result[$value['test']][] = $value;
        }</code>

结果是一个三维数组

Statement:
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