//################# 示例 ################# $arr = array( array( 'name' => '学习', 'size' => '1235', 'type' => 'jpe', 'time' => '1921-11-13', 'class' => 'dd', ), array( 'name' => '中国功夫', 'size' => '153', 'type' => 'jpe', 'time' => '2005-11-13', 'class' => 'jj', ), array( 'name' => '编程', 'size' => '35', 'type' => 'gif', 'time' => '1997-11-13', 'class' => 'dd', ), array( 'name' => '中国功夫', 'size' => '65', 'type' => 'jpe', 'time' => '1925-02-13', 'class' => 'yy', ), array( 'name' => '中国功夫', 'size' => '5', 'type' => 'icon', 'time' => '1967-12-13', 'class' => 'rr', ), ); echo '<pre>'; print_r($arr); echo '<br>'; //注意:按照数字方式排序时 153 比 65 小 $temp = sortArr($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING"); print_r($temp); echo '</pre>';
3. 多维数组转一维数组
function rebuild_array($arr){ //rebuild a array static $tmp=array(); for($i=0; $i<count($arr); $i++){ if(is_array($arr[$i])){ rebuild_array($arr[$i]); }else{ $tmp[]=$arr[$i]; } } return $tmp; }
实例:
$arr=array('123.html','456.html',array('dw.html','fl.html',array('ps.html','fw.html')),'ab.html'); // 定义一个三维数组,用来检测我们的函数 echo '<pre>'; print_r(rebuild_array($arr)); echo '</pre>';
4. 从数组中删除空白的元素
function array_remove_empty(&$arr, $trim = true) { foreach ($arr as $key => $value) { if (is_array($value)) { array_remove_empty($arr[$key]); } else { $value = trim($value); if ($value == '') { unset($arr[$key]); } elseif ($trim) { $arr[$key] = $value; } } } }
实例:
$a = array(array(3),2,'',array('',23),0); array_remove_empty($a); printr($a); // printr 函数请在本文中找
5. 获取多维数组下特定键下的值,并生成一维数组
function getall_by_key(array $arr, $key){ if (!trim($key)) return false; preg_match_all("/\"$key\";\w{1}:(?:\d+:|)(.*?);/", serialize($arr), $output); return $output[1]; } $testArr = array('111'=> 100, 'out'=> 200, '333' => array('out' => 310, array(321, 322, 'out' => "this is a test"))); printr(getall_by_key($testArr, 'out'));