Php array filter functions: array_filter and array_unique

Source: Internet
Author: User
In php, I will introduce two commonly used array filter functions, array_filter and array_unique. one is to filter array null values and the other is to filter array repeated values. let's take a look

In php, I will introduce two commonly used array filter functions, array_filter and array_unique. one is to filter array null values and the other is to filter array repeated values. let's take a look at them now.

Syntax:Array_filter (array, function)

An array is required. it specifies the input array and the name of the function custom function. if it is null, all elements whose values are flase are filtered out. the code is as follows:

  1. Function odd ($ var ){
  2. Return ($ var & 1 );
  3. }
  4. Function even ($ var ){
  5. Return (! ($ Var & 1 ));
  6. }
  7. $ Array1 = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5 );
  8. $ Array2 = array (6, 7, 8, 9, 10, 11, 12 );
  9. Echo "Odd: n ";
  10. Print_r (array_filter ($ array1, "odd "));
  11. Echo "Even: n ";
  12. Print_r (array_filter ($ array2, "even "));
  13. /*
  14. Odd:
  15. Array
  16. (
  17. [A] => 1
  18. [C] => 3
  19. [E] => 5
  20. )
  21. Even:
  22. Array
  23. (
  24. [0] => 6
  25. [2] => 8
  26. [4] => 10
  27. [6] => 12
  28. )
  29. */

You can use the foreach method or the array_unique method to filter out the repeated values in the PHP array and remove the repeated values in the array. the following code uses both methods:

  1. $ ArrF = array ();
  2. $ ArrS = array ();
  3. $ IntTotal = 100;
  4. $ IntRand = 10;
  5. For ($ I = 0; $ I <$ intTotal; $ I ++)
  6. {
  7. $ ArrF [] = rand (1, $ intRand );
  8. $ ArrS [] = rand (1, $ intRand );
  9. }
  10. $ ArrT = array_merge ($ arrF, $ arrS );
  11. $ ArrRF = array ();
  12. $ IntStart = time ();
  13. Foreach ($ arrT as $ v)
  14. {
  15. If (in_array ($ v, $ arrRF ))
  16. {
  17. Continue;
  18. }
  19. Else
  20. {
  21. $ ArrRF [] = $ v;
  22. }
  23. }
  24. $ IntEnd = time ();
  25. $ IntTime = $ intEnd-$ intStart;
  26. Echo "With Continue, Spend time: $ intTime
    ";
  27. $ IntStart1 = time ();
  28. $ ArrRS = array_unique ($ arrT );
  29. $ IntEnd2 = time ();
  30. $ IntTime2 = $ intEnd2-$ intStart1;
  31. Echo "With array_unique function, Spend time :( $ intTime2 )";
  32. Echo"
    "; 
  33. Print_r ($ arrT );
  34. Print_r ($ arrRF );
  35. Print_r ($ arrRS );
  36. Echo"";
  37. ?>

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

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

Therefore, we can draw a conclusion that when the array capacity is small and it is about 1000 or less, the execution efficiency of the two is similar. when the array capacity is large (the specific value should be, I didn't perform a detailed test. if you are interested, check this value.) as $ intRand increases, array_unique performs better. I don't use the $ intTotal/$ intRand ratio because, it doesn't feel like a proportional change, but the larger the ratio is, the better the array_unique performance is.

To sum up, we recommend that you use array_unuique when filtering array duplicate values. when the array is not large, the efficiency of the two is the same, while the use of array_unique will of course reduce your code by several lines at once, function performance is better when the array capacity is too large

Two-dimensional array deduplication function

PHP array deduplication has a built-in function array_unique (), but php's array_unique function is only applicable to one-dimensional arrays and not to multidimensional arrays. The following provides a two-dimensional array array_unique function, the code is as follows:

  1. Function unique_arr ($ array2D, $ stkeep = false, $ ndformat = true)
  2. {
  3. // Determine whether the primary array key is retained (the primary array key can be non-numeric)
  4. If ($ stkeep) $ stArr = array_keys ($ array2D );
  5. // Determine whether the secondary array key is retained (all secondary array keys must be the same)
  6. If ($ ndformat) $ ndArr = array_keys (end ($ array2D ));
  7. // Dimensionality reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
  8. Foreach ($ array2D as $ v ){
  9. $ V = join (",", $ v );
  10. $ Temp [] = $ v;
  11. }
  12. // Remove the duplicate string, that is, the duplicate one-dimensional array.
  13. $ Temp = array_unique ($ temp );
  14. // Re-assemble the split array
  15. Foreach ($ temp as $ k => $ v)
  16. {
  17. If ($ stkeep) $ k = $ stArr [$ k];
  18. If ($ ndformat)
  19. {
  20. $ TempArr = explode (",", $ v );
  21. Foreach ($ tempArr as $ ndkey => $ ndval) $ output [$ k] [$ ndArr [$ ndkey] = $ ndval;
  22. }
  23. Else $ output [$ k] = explode (",", $ v );
  24. }
  25. Return $ output;
  26. }
  27. Test
  28. $ Array2D = array ('first' => array ('title' => '123', 'date' => '123 '), 'second' => array ('title' => '123', 'date' => '123 '), 'third' => array ('title' => '123456', 'date' => '123456 '));
  29.  
  30. Print_r ($ array2D );
  31. Print_r (unique_arr ($ array2D, true ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.