onlinecode

How to remove duplicate values from a multi-dimensional array in PHP

How to remove duplicate values from a multi-dimensional array in PHP

In this post we will give you information about How to remove duplicate values from a multi-dimensional array in PHP. Hear we will give you detail about How to remove duplicate values from a multi-dimensional array in PHPAnd how to use it also give you demo for it if it is necessary.

Whenever you need to remove duplicate values from simple array or multi-dimensional array in PHP and you can follow with given code to get unique values in array.

array_unique() function are used to remove duplicate data from given array.
Now question is what value will be removed if there are two or more array values are same, in that scenario it will keep first appearance value in array and removed others.

There are two parameters which will be pass in array_unique(), first specifying an array which is required and second is optioanl specifying sorting type.

Optional parameters sortingtype have possible values :

  • SORT_STRING – Default
  • SORT_NUMERIC
  • SORT_REGULAR
  • SORT_LOCALE_STRING

Remove duplicate values from an array in PHP

Have a look on given example to remove duplicate values from an array.

  1. <?php
  2. $a=array("a"=>"onlinecode","b"=>"demo","c"=>"onlinecode");
  3. echo"<pre>";
  4. print_r(array_unique($a));
  5. ?>
<?php
$a=array("a"=>"onlinecode","b"=>"demo","c"=>"onlinecode");
echo "<pre>";
print_r(array_unique($a));
?>

Output of array before removing duplicate values from a multi-dimensional array in PHP.

  1. Array
  2. (
  3. [a]=> onlinecode
  4. [b]=> demo
  5. [c]=> onlinecode
  6. )
Array
(
    [a] => onlinecode
    [b] => demo
     => onlinecode
)

Output of array after removing duplicate values from an array in PHP.

  1. Array
  2. (
  3. [a]=> onlinecode
  4. [b]=> demo
  5. )
Array
(
    [a] => onlinecode
    [b] => demo
)

Now you will know how to remove duplicate values from a multi-dimensional array.

Have a look on given example to remove duplicate values from a multi-dimensional array.

  1. <?php
  2. $input=Array(Array('ab','cd'),Array('ef','gh'),Array('ij','kl'),Array('ab','cd'));
  3. echo"<pre>";
  4. print_r($input);
  5. $input=array_map("unserialize",array_unique(array_map("serialize",$input)));
  6. print_r($input);
  7. ?>
<?php
$input=Array(Array('ab','cd'),Array('ef','gh'),Array('ij','kl'),Array('ab','cd'));
echo "<pre>";
print_r($input);
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
print_r($input);
?>

Output of array before removing duplicate values from a multi-dimensional array in PHP.

  1. Array
  2. (
  3. []=> Array
  4. (
  5. []=> ab
  6. [1]=> cd
  7. )
  8. [1]=> Array
  9. (
  10. []=> ef
  11. [1]=> gh
  12. )
  13. [2]=> Array
  14. (
  15. []=> ij
  16. [1]=> kl
  17. )
  18. [3]=> Array
  19. (
  20. []=> ab
  21. [1]=> cd
  22. )
  23. )
Array
(
    [0] => Array
        (
            [0] => ab
            [1] => cd
        )

    [1] => Array
        (
            [0] => ef
            [1] => gh
        )

    [2] => Array
        (
            [0] => ij
            [1] => kl
        )

    [3] => Array
        (
            [0] => ab
            [1] => cd
        )

)

Output of array after removing duplicate values from a multi-dimensional array in PHP.

  1. Array
  2. (
  3. []=> Array
  4. (
  5. []=> ab
  6. [1]=> cd
  7. )
  8. [1]=> Array
  9. (
  10. []=> ef
  11. [1]=> gh
  12. )
  13. [2]=> Array
  14. (
  15. []=> ij
  16. [1]=> kl
  17. )
  18. )
Array
(
    [0] => Array
        (
            [0] => ab
            [1] => cd
        )

    [1] => Array
        (
            [0] => ef
            [1] => gh
        )

    [2] => Array
        (
            [0] => ij
            [1] => kl
        )

)


Find the Duplicate values in array using PHP

  1. <?php
  2. $array=array("onlinecode","demo","onlinecode");
  3. $count_array=array_count_values($array);
  4. echo"<pre>";
  5. print_r($count_array);
  6. $results=array();
  7. foreach($count_arrayas$key=>$val){
  8. if($val==1){
  9. $results[$key]='unique';
  10. }
  11. else{
  12. $results[$key]='duplicate';
  13. }
  14. }
  15. echo"<pre>";
  16. print_r($results);
  17. ?>
<?php
$array=array("onlinecode","demo","onlinecode");
$count_array = array_count_values($array);

echo "<pre>"; 
print_r($count_array);

$results = array();
foreach($count_array as $key=>$val){
   if($val == 1){
      $results[$key] = 'unique';
   }
   else{
      $results[$key] = 'duplicate';
   }
}

echo "<pre>";
print_r($results);
?>

Output :

  1. Array
  2. (
  3. [onlinecode]=>2
  4. [demo]=>1
  5. )
  6. Array
  7. (
  8. [onlinecode]=> duplicate
  9. [demo]=> unique
  10. )
Array
(
    [onlinecode] => 2
    [demo] => 1
)
Array
(
    [onlinecode] => duplicate
    [demo] => unique
)

array_count_values counts all values of an array.

Label :

PHP

How To

Hope this code and post will helped you for implement How to remove duplicate values from a multi-dimensional array in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version