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 even($var) {
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
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
// Determine whether to retain the first-level array key (the first-level array key can be non-numeric) //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...
Related labels:
source:php.cn
Previous article:PHP code to implement 301 jump page example_PHP tutorial
Next article:PHP prints out the hexadecimal value of a string_PHP tutorial
Statement of this Website
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|