Answers for "php filter duplicates from array"

PHP
18

php remove duplicates from array

<?php
$fruits_list = array('Orange',  'Apple', ' Banana', 'Cherry', ' Banana');
$result = array_unique($fruits_list);
print_r($result);
?>
  
Output:

Array ( [0] => Orange [1] => Apple [2] => Banana [3] => Cherry )
Posted by: Guest on March-04-2020
1

array_unique

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

Array
(
    [a] => green
    [0] => red
    [1] => blue
)
Posted by: Guest on May-26-2020
1

remove duplicate values in array php

<?php
$list_programming_language = array('C#',  'C++', 'PHP', 'C#', 'PHP');
$result = array_unique($list_programming_language);
print_r($result);
?>
  
// ==> 'C#',  'C++', 'PHP'
Posted by: Guest on March-11-2021
1

remove duplicate value from array

let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];

console.log(uniqueChars);
Posted by: Guest on October-29-2020
0

filter duplicate associative array by value check in php

<?php
function unique_key($array,$keyname){

 $new_array = array();
 foreach($array as $key=>$value){

   if(!isset($new_array[$value[$keyname]])){
     $new_array[$value[$keyname]] = $value;
   }

 }
 $new_array = array_values($new_array);
 return $new_array;
}

// Array
$student_arr[] = array("name" => "Yogesh Singh","age"=>24);
$student_arr[] = array("name" => "Sonarika Bhadoria","age"=>24);
$student_arr[] = array("name" => "Anil Singh","age" => 23);
$student_arr[] = array("name" => "Mayank Patidar","age" => 25);
$student_arr[] = array("name" => "Anil Singh","age" => 19);

// Remove duplicate value according to 'name'
$student_unique_arr = unique_key($student_arr,'name');

echo "<pre>";
print_r($student_arr);
echo "</pre>";

echo "<b>Array after remove duplicate key</b>";
echo "<pre>";
print_r($student_unique_arr);
echo "</pre>";
Posted by: Guest on March-31-2021
-1

php remove duplicates from string

$str = implode(',',array_unique(explode(',', $str)));
Posted by: Guest on June-24-2020

Code answers related to "php filter duplicates from array"

Browse Popular Code Answers by Language