加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

用到的php中数组重组和排序

(2011-10-25 17:39:40)
标签:

php

数组的组合

it

分类: php那些事
#数组重组#
原数组$result:
array{
 
[0]=>
  {
    
["user_email"]=>"123@qq.com"
    
["company_id"]=>"2"
  
}
  
[1]=>
   {
   
  ["user_email"]=>"123@qq.com"
  
  ["company_id"]=>"3"
  
}
  
[2]=>
  {
   
  ["user_email"]=>"123@qq.com"
   
  ["company_id"]=>"4"
  
}
 
[3]=>
 {
  
  ["user_email"]=>"123456@qq.com"
   
  ["company_id"]=>"2"
 
  }


}

组合后的数组$merge

array(3) { 

 [2]=> 

 {
    ["user_email"]=> 

         

              [0]=> "123@qq.com" 

           [1]=> "123456@qq.com"
    } 

         ["company_id"]=>  "2"
  } 

 [3]=>{ 

    ["user_email"]=>  {
      [0]=>  "123@qq.com"
    }

  ["company_id"]=>  "3"
  }   

[4]=>  {
    ["user_email"]=> 

   {
      [0]=>
      string(10) "123@qq.com"
    }

   ["company_id"]=>
    string(1) "4"
  } 

}



方法一:
for($i=0;$i<count($result);$i++){
    $j=$result[$i]['company_id'];
    if(empty($merge[$j])){
        $merge[$j]=array('user_email'=>array($result[$i]['user_email']),'company_id'=>$result[$i]['company_id']);
        }else{
 $merge[$j]['user_email'][]=$result[$i]['user_email']; 
        }
   }
方法二:使用方法
 function array_extend($a, $b) {
    foreach($b as $k=>$v) {
        if( is_array($v) {
            if( !isset($a[$k]) {
                $a[$k] $v;
            else {
                $a[$k] array_extend($a[$k], $v);
            }
        else {
            $a[$k] $v;
        }
    }
    return $a;
#数组重组 end#

#多维数组排序#
function sortArr($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")  
{  
        if(!is_array($ArrayData)) return $ArrayData;  
          
        // Get args number.  
        $ArgCount = func_num_args();  
        // Get keys to sort by and put them to SortRule array.  
        for($I = 1;$I < $ArgCount;$I ++)  
        {  
            $Arg = func_get_arg($I);  
            if(!eregi("SORT",$Arg))  
            {  
                $KeyNameList[] = $Arg;  
                $SortRule[]    = '$'.$Arg;  
            }  
            else $SortRule[]   = $Arg;  
        }  
        // Get the values according to the keys and put them to array.  
        foreach($ArrayData AS $Key => $Info)  
        {  
            foreach($KeyNameList AS $KeyName) ${$KeyName}[$Key] = strtolower($Info[$KeyName]);  
        }  
          
        // Create the eval string and eval it.  
        $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';  
        eval_r($EvalString);  
        return $ArrayData;  
}  
$newallpic = sortArr($allpic,"create_time","SORT_DESC");
#多维数组排序 end#

#打乱二维数组#
    function shuffle_assoc($list) { 
        if (!is_array($list)) return $list; 
        $keys = array_keys($list); 
        shuffle($keys); 
        $random = array(); 
        foreach ($keys as $key) 
        $random[$key] = $list[$key]; 
        return $random; 
    }
#打乱二维数组 End#

#打乱多维数组#
function rec_assoc_shuffle($array)
{
$ary_keys = array_keys($array);
$ary_values = array_values($array);
shuffle($ary_values);
foreach($ary_keys as $key => $value) {
if (is_array($ary_values[$key]) AND $ary_values[$key] != NULL) {
 $ary_values[$key] = rec_assoc_shuffle($ary_values[$key]);
}
$new[$value] = $ary_values[$key];
}
return $new;
}
#打乱多维数组 end#

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有