Home > php教程 > PHP源码 > body text

二维数组的‘分组’排序

PHP中文网
Release: 2016-05-25 17:09:01
Original
977 people have browsed it


$val){
				  asort($new[$key]);//分别对每一组的数据进行排序;
				  $tmp=$new[$key];//将排序后的数据赋值给一个临时数组;
				  $tmp[]=$key;//将原来数组的键值加入到临时数组的末尾,为后期的修改键值的步骤做准备;
				  $tmp2=$new[$key];
				  $a=array_shift($tmp2)*100;//将每一组的第一项(最小项)数据取出来,以aa组为例子,$a此时的值就为33;
                                  if(array_key_exists($a,$aa)){++$q_count;$a=$a+$q_count;}
				  $aa[$a]=$tmp;//新建一个数组,以$aa(比如33)为键值;
				  ksort($aa);//按照键值排序
		  }
		  foreach($aa as $key=>$val){//按照键值排序的数组,已经失去了原来的键值,比如aa/bb/cc/dd之类的,好在我们之前已经将键值存入了临时数组的末尾;
			  $b=array_pop($aa[$key]);//将键值取出来;
			  $bb[$b]=$aa[$key];//从新建立一个数组,使用之前的键值(aa/bb/cc)
		  }
		  return $bb;//返回
}
			
$c=arr_group_sort($new);    
print_r($new);//原数组;
print_r($c);//分组排序后的数组;
            ?>
Copy after login


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!