一、两个二维数组的合并

$a=Array(0 => Array("id" => 66,"class_name" => "www.iiwnet.com"),1 => Array("id" => 67,"class_name" => "linux" ));
$b=Array(0 => Array("class_count"=> 8),1 => Array("class_count" => 2));

方法一:

php提供了一个方便的函数:

array_merge_recursive($as_activity,$array['data']['as_activity']);//合并两个数组

方法二:

自己手动实现

$arr2 = array();
	     
	        $n = 0;
        	foreach($a as $k => $v){
        		foreach($v as $k1 => $v1){
                	$arr2[$n][$k1] = $v1;
        		}
            	$n++;
            }
        	foreach($b as $k => $v){
        		foreach($v as $k1 => $v1){
                	$arr2[$n][$k1] = $v1;
            	}
            	$n++;
            }

输出:

array(4) { [0] => array(2) { ["id"] => int(66) ["class_name"] => string(14) "www.iiwnet.com" } [1] => array(2) { ["id"] => int(67) ["class_name"] => string(5) "linux" } [2] => array(1) { ["class_count"] => int(8) } [3] => array(1) { ["class_count"] => int(2) }}
2、php对二维数组按其中某个值,进行排序
$sort = array(  
		        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序  
		        'field'     => 'publish_date',       //排序字段  
			);  
			$arrSort = array();
			foreach($result AS $uniqid => $row){  		//$result
			    foreach($row AS $key=>$value){  
			        $arrSort[$key][$uniqid] = $value;  
			    }  
			}  
			if($sort['direction']){
			    array_multisort($arrSort[$sort['field']], constant($sort['direction']), $result);  
			}

3、其他

unset($as_activity[0]);//删除数组元素,删除后的数组,不是索引数组
$as_activity = array_values($as_activity);//重建索引数组
array_merge_recursive($as_activity,$array['data']['as_activity']);//合并两个数组
array_column($array,'key')//(二维数组,二维数组的key)得到二维数组中的某一列值
$res = array(); //想要的结果
foreach ($result as $k => $v) {
    $res[$v['time']][] = $v;
}//二维数组按相同字段合并




GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐