<?php
$test[0] = array(
‘id‘ => ‘17‘,
‘name‘ => "儿童",
‘ra‘ => "456"
);
$test[1] = array(
‘id‘ => ‘17‘,
‘name‘ => "sad儿童",
‘ra‘ => "789"
);
$test[2] = array(
‘id‘ => ‘16‘,
‘name‘ => "撒的",
‘ra‘ => "235"
);
$test[3] = array(
‘id‘ => ‘19‘,
‘name‘ => "女",
‘ra‘ => "235"
);
//print_r($test);
//去除二维数组重复值,默认重复保留前面的值
/*
*array 二维数组
*keyid 需要判断是否重复的项目
*desc 为true时保留后面的
*/
function array_repeat($array,$keyid="id",$desc=false)
{
$array = array_values($array);
//倒叙排列数
if($desc)
{
$array = array_rsort($array);
}
//提取需要判断的项目变成一维数组
$a = array_tq($array,$keyid);
//去除一维数组重复值
$a = array_unique($a);
//提取二维数组项目值
foreach($array[0] AS $key=>$value)
{
$akey[] = $key;
}
//重新拼接二维数组
foreach($akey AS $key=>$value)
{
$b = array_tq($array,$value);
foreach($a AS $key2=>$value2)
{
$c[$key2][$value] = $b[$key2];
}
}
if($desc)
{
$c = array_rsort($c);
}
return $c;
}
//数组倒叙
function array_rsort($arr)
{
if(is_array($arr))
{
$flag = false;
//一维数组
if(count($arr) == count($arr,1))
{
$flag = true;
$i = 0;
//转换成二维数组
foreach($arr AS $key=>$value)
{
$a[$i]["okey"] = $key;
$a[$i]["value"] = $value;
$i++;
}
$arr = $a;
}
//多维数组
else
{
foreach($arr AS $key=>$value)
{
$value["okey"] = $key;
$array[] = $value;
}
$arr = $array;
}
//倒叙并还原key值
$aa = count($arr)-1;
for($i=0;$i<count($arr);$i++)
{
$b[$arr[$aa]["okey"]] = $arr[$aa];
$aa--;
}
//重构一维数组
if($flag == true)
{
foreach($b AS $key=>$value)
{
$c[$value["okey"]] = $value["value"];
}
}
//多维数组去除临时key值
else
{
foreach($b AS $key=>$value)
{
unset($value["okey"]);
$c[$key] = $value;
}
$c = $b;
}
return $c;
}
}
//提取二维数组项目
function array_tq($array,$aval="")
{
foreach($array AS $key=>$value)
{
$result[] = $value[$aval];
}
return $result;
}
$test = array_repeat($test,"id");
print_r($test);
?>
原文地址:https://www.cnblogs.com/ovsexia/p/10630170.html