2 array_values($array) //返回所有键值
3 $result=array_reverse($input); //将数组颠倒,不保留键名
4 $result_keyed=array_reverse($input,true); //将数组颠倒,保留键名
5 array_keys($array,"blue"); //返回值为blue的键名
6 PHP二维数组去重复项函数
PHP数组去除重复项 有个内置函数array_unique (),但是php的 array_unique函数只适用于一维数组,对多维数组并不适用,以下提供一个二维数组 的 array_unique函数
Php代码
function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
// 判断是否保留一级数组键 (一级数组键可以为非数字)
if($stkeep) $stArr = array_keys($array2D);
// 判断是否保留二级数组键 (所有二级数组键必须相同)
if($ndformat) $ndArr = array_keys(end($array2D));
//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
foreach ($array2D as $v){
$v = join(",",$v);
$temp[] = $v;
}
//去掉重复的字符串,也就是重复的一维数组
$temp = array_unique($temp);
//再将拆开的数组重新组装
foreach ($temp as $k => $v)
{
if($stkeep) $k = $stArr[$k];
if($ndformat)
{
$tempArr = explode(",",$v);
foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
}
else $output[$k] = explode(",",$v);
}
return $output;
}
Php代码
$array2D = array('first'=>array('title'=>'1111','date'=>'2222'),'second'=>array('title'=>'1111','date'=>'2222'),'third'=>array('title'=>'2222','date'=>'3333')); print_r($array2D); print_r(unique_arr($array2D,true));
$array2D = array('first'=>array('title'=>'1111','date'=>'2222'),'second'=>array('title'=>'1111','date'=>'2222'),'third'=>array('title'=>'2222','date'=>'3333')); print_r($array2D);print_r(unique_arr($array2D,true));
二、将多维数组转换为一维,二维数组
//将多维转换在一维
function rebuild_array($arr){ //rebuild a array
static $tmp=array();
foreach($arr as $key=>$val){
if(is_array($val)){
$this->rebuild_array($val);
}else{
$tmp[] = $val;
}
}
return $tmp;
}
//将三维数据数组转换为二维
function rebuild_arrays($arr){ //rebuild a array
static $tmp=array();
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
$tmp[] = $v;
}
}
return $tmp;
}
三、去掉数组中的最后一项
$name = array("orange","banana", "apple"," ");
array_pop ($name); //弹出尾部
print_r($name); //$name = array("orange","banana", "apple");
function multisort(&$array, $key_name, $sort_order = 'SORT_ASC', $sort_type = 'SORT_REGULAR') { if (!is_array($array)) { return $array; } // Get args number. $arg_count = func_num_args(); // Get keys to sort by and put them to SortRule array. for ($i = 1; $i < $arg_count; $i++) { $arg = func_get_arg($i); if (!preg_match('/SORT/', $arg)) { $key_name_list[] = $arg; $sort_rule[] = '$'.$arg; } else { $sort_rule[] = $arg; } } // Get the values according to the keys and put them to array. foreach ($array as $key => $info) { foreach ($key_name_list as $key_name) { ${$key_name}[$key] = $info[$key_name]; } } // Create the eval string and eval it. $eval_str = 'array_multisort('.implode(',', $sort_rule).', $array);'; eval($eval_str); return $array; }
调用方法:$this->multisort($u, "address");dump($u);