Home  >  Article  >  Backend Development  >  PHP array filter function array_filter and array_unique_PHP tutorial

PHP array filter function array_filter and array_unique_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:131243browse

In PHP, I will introduce to you two commonly used array filtering functions, array_filter and array_unique. One is to filter array empty values, and the other is to filter array duplicate values. Let’s take a look together now.

Grammar
array_filter(array,function)
Parameter Description
array Required. Specifies the input array.
function The name of the custom function, when it is empty, all elements whose value is false are filtered out

The code is as follows Copy code
 代码如下 复制代码


function odd($var) {
return($var & 1);
}

function even($var) {
return(!($var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
 
echo "Odd :n";
print_r(array_filter($array1, "odd"));
echo "Even:n";
print_r(array_filter($array2, "even"));
/*
 
Odd :
Array
(
   
[a] => 1
   
[c] => 3
   
[e] => 5
)
Even:
Array
(
   
[0] => 6
   
[2] => 8
   
[4] => 10
   
[6] => 12
)
*/

function odd($var) { Return($var & 1);

}

function even($var) {
代码如下 复制代码

$arrF = array();
$arrS = array();
$intTotal = 100;
$intRand = 10;
for($i=0; $i < $intTotal; $i++)
{
$arrF[] = rand(1, $intRand);
$arrS[] = rand(1, $intRand);
}
$arrT = array_merge($arrF, $arrS);
$arrRF = array();
$intStart = time();
foreach($arrT as $v)
{
if(in_array($v, $arrRF))
{
continue;
}
else
{
$arrRF[] = $v;
}
}
$intEnd = time();
$intTime = $intEnd-$intStart;
echo "With Continue,Spend time:$intTime
";
$intStart1 = time();
$arrRS = array_unique($arrT);
$intEnd2 = time();
$intTime2 = $intEnd2-$intStart1;
echo "With array_unique function,Spend time:($intTime2)";
echo "

";<br>
print_r($arrT);<br>
print_r($arrRF);<br>
print_r($arrRS);<br>
echo "
";

?>

Return(!($var & 1)); } $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd :n"; print_r(array_filter($array1, "odd")); echo "Even:n"; print_r(array_filter($array2, "even")); /* Odd : Array (   [a] => 1   [c] => 3   [e] => 5 ) Even: Array (   [0] => 6   [2] => 8   [4] => 10   [6] => 12 ) */ Filter out duplicate values ​​in PHP arrays To remove duplicate values ​​from an array, you can use the foreach method or the array_unique method. The following code uses both methods
The code is as follows Copy code
<🎜>$arrF = array();<🎜> $arrS = array();<🎜> $intTotal = 100;<🎜> $intRand = 10;<🎜> for($i=0; $i < $intTotal; $i++)<🎜> {<🎜> $arrF[] = rand(1, $intRand);<🎜> $arrS[] = rand(1, $intRand);<🎜> }<🎜> $arrT = array_merge($arrF, $arrS);<🎜> $arrRF = array();<🎜> $intStart = time();<🎜> foreach($arrT as $v)<🎜> {<🎜> if(in_array($v, $arrRF))<🎜> {<🎜> continue;<🎜> }<🎜> else<🎜> {<🎜> $arrRF[] = $v;<🎜> }<🎜> }<🎜> $intEnd = time();<🎜> $intTime = $intEnd-$intStart;<🎜> echo "With Continue,Spend time:$intTime
"; $intStart1 = time(); $arrRS = array_unique($arrT); $intEnd2 = time(); $intTime2 = $intEnd2-$intStart1; echo "With array_unique function,Spend time:($intTime2)"; echo "
";
print_r($arrT);
print_r($arrRF);
print_r($arrRS);
echo "
"; ?>


When $intTotal is relatively small, for example, within 1000, the value of $intRand basically does not affect the result, and the execution time of both is similar.

When testing $intTotal is greater than 10000 and $intRand is 100, the efficiency of using array_unique is higher than that of foreach loop judgment. $intRand=10, the execution time of the two is consistent.

Therefore, it can be concluded that when the array capacity is not large, probably within 1000, the execution efficiency of using the two is similar.

When the array capacity is relatively large (I have not tested the specific value, you can determine this value if you are interested), as $intRand gradually increases, array_unique performs better, I do not use $ The reason for the ratio of intTotal/$intRand is that it does not feel proportional to the change, but it basically follows that the larger the ratio, the better the performance of array_unique.

To sum up, when filtering duplicate values ​​in an array, it is recommended to use array_unuique. When the array is small, the two are equally efficient. Using array_unique will of course reduce your code by several lines. When the array capacity is too large, , the function performs better


Two-dimensional array deduplication function

PHP array removes duplicates. There is a built-in function array_unique (), but PHP's array_unique function only applies to one-dimensional arrays, not multi-dimensional arrays. The following provides an array_unique function for a two-dimensional array

The code is as follows
 代码如下 复制代码

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;
}

测试

$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));

Copy code
function unique_arr($array2D,$stkeep=false,$ndformat=true) {

// Determine whether to retain the first-level array key (the first-level array key can be non-numeric)

if($stkeep) $stArr = array_keys($array2D); // Determine whether to retain the secondary array keys (all secondary array keys must be the same) if($ndformat) $ndArr = array_keys(end($array2D)); //Dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected with commas foreach ($array2D as $v){ $v = join(",",$v); $temp[] = $v; } //Remove repeated strings, that is, repeated one-dimensional arrays $temp = array_unique($temp);
//Reassemble the disassembled array 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; } Test $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)); http://www.bkjia.com/PHPjc/633131.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633131.htmlTechArticleIn php, I will introduce to you two commonly used array filtering functions, array_filter and array_unique. One is to filter the array. Null value, one is to filter the duplicate value of the array, let's do it together now...
Statement:
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