Ejemplo 1: obtener un valor duplicado de array php
$arr =array(1,4,6,1,8,9,4,6);
$unique =array_unique($arr);
$duplicates =array_diff_assoc($arr, $unique);print_r($duplicates);Array([3]=>1[6]=>4[7]=>6)
Ejemplo 2: php get keys de valores duplicados en array
<?php
/**
Examples of these functions:
array_unique,
array_diff_assoc,
array_diff,
array_keys,
array_intersect
Examle with an array:
*/
$array =array('a','a','b','c','d');// Unique values
$unique =array_unique($array);// Duplicates
$duplicates =array_diff_assoc($array, $unique);// Unique values
$result =array_diff($unique, $duplicates);// Get the unique keys
$unique_keys =array_keys($result);// Get duplicate keys
$duplicate_keys =array_keys(array_intersect($array, $duplicates));
Ejemplo 3: php comprueba si hay duplicados en array
functionhas_dupes($array)
$dupe_array =array();foreach($array as $val)if(++$dupe_array[$val]>1)returntrue;returnfalse;
Ejemplo 4: filtrar asociativo duplicado array por verificación de valor en php
<?php
functionunique_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 ""
;print_r($student_arr);
echo "
";
echo "Array after remove duplicate key";
echo "
";print_r($student_unique_arr); echo "
";
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)