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

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

WBOY
Release: 2016-06-06 20:13:31
Original
890 people have browsed it

<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>
Copy after login
Copy after login

类似上面的数组,需要把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>
Copy after login
Copy after login

类似上面的数组,需要把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>
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