欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-25 17:25:51
...
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',
      )
)

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

回复内容:

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',
      )
)

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

其实很简单:

$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;
        }

结果是一个三维数组

相关标签: php thinkphp