Php method for judging and removing repeated data from Arrays

Source: Internet
Author: User
Php method for judging and removing repeated data from Arrays

  1. If (count ($ array )! = Count (array_unique ($ array ))){
  2. Echo 'This array has repeated values ';
  3. }
  4. ?>

PHP removes repeated array data

  1. $ Input = array ("a" => "green", "", "red", "B" => "green", "", "blue ", "red", "c" => "witer", "hello", "witer ");

  2. // $ Result = array_unique ($ input); // remove duplicate elements
  3. $ Result = a_array_unique ($ input); // only a single element is left.
  4. Foreach ($ result as $ aa)
  5. {
  6. Echo $ aa ."
    ";
  7. }
  8. Function multi_unique ($ array ){
  9. Foreach ($ array as $ k => $ na)
  10. $ New [$ k] = serialize ($ na );
  11. $ Uniq = array_unique ($ new );
  12. Foreach ($ uniq as $ k => $ ser)
  13. $ New1 [$ k] = unserialize ($ ser );
  14. Return ($ new1 );
  15. }

  16. Function a_array_unique ($ array) // write better

  17. {
  18. $ Out = array ();
  19. Foreach ($ array as $ key => $ value ){
  20. If (! In_array ($ value, $ out ))
  21. {
  22. $ Out [$ key] = $ value;
  23. }
  24. }
  25. Return $ out;
  26. }
  27. ?>

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 array_unique function of a two-dimensional array is implemented 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 cases:

  1. $ Array2D = array ('first' => array ('title' => '123', 'date' => '123 '), 'second' => array ('title' => '123', 'date' => '123 '), 'third' => array ('title' => '123456', 'date' => '123456 '));
  2. Print_r ($ array2D );
  3. Print_r (unique_arr ($ array2D, true ));
  4. ?>

Php checks whether the same value array_unique exists in the array.12. Next page

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.