码迷,mamicode.com
首页 > 编程语言 > 详细

PHP 二维数组合并(二)

时间:2015-03-19 06:11:03      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

例如有如下数组:

$arr = array(
        0=>array(
            ‘product‘=>‘120‘,
            ‘type‘=>0
        ),
        1=>array(
            ‘product‘=>‘120‘,
            ‘type‘=>0        
        ),
        2=>array(
            ‘product‘=>‘120‘,
            ‘type‘=>1        
        ),
        3=>array(
            ‘product‘=>‘121‘,
            ‘type‘=>0        
        ),
        4=>array(
            ‘product‘=>‘121‘,
            ‘type‘=>1        
        ),
        5=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>2        
        ),    
        6=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>3        
        ),    
        7=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>3        
        ),    
        8=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>3        
        ),                        
        9=>array(
            ‘product‘=>‘123‘,
            ‘type‘=>2        
        ),                    
);

要求:

① 相同 product 值的数组要合并成一个数组,并计算具有相同 product 的数组的个数;

② 计算相同 product 值的数组的 type,相同的 type 保留一个,并计该 type 相加的数量;不同的 type 也进行保留,数量为1

即将上面的数组转换为下面的数组:

array
  2 => 
    array
      ‘product‘ => string ‘120‘ (length=3)
      ‘count‘ => int 3
      ‘newtype‘ => 
        array
          0 => int 2
          1 => int 1
  4 => 
    array
      ‘product‘ => string ‘121‘ (length=3)
      ‘count‘ => int 2
      ‘newtype‘ => 
        array
          0 => int 1
          1 => int 1
  8 => 
    array
      ‘product‘ => string ‘122‘ (length=3)
      ‘count‘ => int 4
      ‘newtype‘ => 
        array
          2 => int 1
          3 => int 3
  9 => 
    array
      ‘product‘ => string ‘123‘ (length=3)
      ‘count‘ => int 1
      ‘newtype‘ => 
        array
          2 => int 1

php:

<?php

$arr = array(
        0=>array(
            ‘product‘=>‘120‘,
            ‘type‘=>0
        ),
        1=>array(
            ‘product‘=>‘120‘,
            ‘type‘=>0        
        ),
        2=>array(
            ‘product‘=>‘120‘,
            ‘type‘=>1        
        ),
        3=>array(
            ‘product‘=>‘121‘,
            ‘type‘=>0        
        ),
        4=>array(
            ‘product‘=>‘121‘,
            ‘type‘=>1        
        ),
        5=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>2        
        ),    
        6=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>3        
        ),    
        7=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>3        
        ),    
        8=>array(
            ‘product‘=>‘122‘,
            ‘type‘=>3        
        ),                        
        9=>array(
            ‘product‘=>‘123‘,
            ‘type‘=>2        
        ),                    
);

var_dump($arr);

//首先对数组排序,例如:order by product asc,type asc
$i = 1;
$j = 1;
$newtype = array();
foreach($arr as $k=>$v){

    if($k < count($arr)-1){

        if($arr[$k][‘product‘] == $arr[$k+1][‘product‘]){

            if($arr[$k][‘type‘] == $arr[$k+1][‘type‘]){
                $j++;
                $newtype[$arr[$k][‘product‘]][$arr[$k+1][‘type‘]] = $j;
            }else{
                $j = 1;
                $newtype[$arr[$k][‘product‘]][$arr[$k+1][‘type‘]] = $j;
            }

            $i++;
            $arr[$k+1][‘count‘] = $i;
            $arr[$k][‘del‘] = 1;
        }else{

            $j = 1;
            $newtype[$arr[$k+1][‘product‘]][$arr[$k+1][‘type‘]] = $j;

            $i = 1;
            $arr[$k+1][‘count‘] = $i;
        }
    }
}


foreach ($arr as $k=>$v) {
    if(@$arr[$k][‘del‘]){
        unset($arr[$k]);
    }
}
echo ‘<hr>‘;
var_dump($arr);
echo ‘<hr>‘;
var_dump($newtype);

foreach($arr as $key=>$val){
    foreach ($newtype as $k => $v) {
        if($arr[$key][‘product‘] == $k){
            $arr[$key][‘newtype‘] = $v;
        }
    }
    unset($arr[$key][‘type‘]);
}

echo ‘<hr>‘;
var_dump($arr);

 

PHP 二维数组合并(二)

标签:

原文地址:http://www.cnblogs.com/dee0912/p/4349231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!