PHP: Check for duplicate values in a multidimensional array
This will remove duplicate items from your array using array_unique()
:
$new_arr = array_unique($arr, SORT_REGULAR);
How to detect duplicate values in multidimensional associative array?
Simple solution is:
<?php
$data = [
[
'name' => 'name 1',
'phone' => '12341234',
'email' => 'test@web.com'
],
[
'name' => 'name 2',
'phone' => '12341234',
'email' => 'test@web1.com'
],
[
'name' => 'name 3',
'phone' => '4322342',
'email' => 'test@web1.com'
],
[
'name' => 'name 4',
'phone' => '1234123423',
'email' => 'test@web1.com'
],
[
'name' => 'name 5',
'phone' => '12341266634',
'email' => 'test@eqweqwweb.com'
],
];
$phones = [];
$emails = [];
foreach ($data as $key => $contact) {
if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
unset($data[$key]);
} else {
$phones[] = $contact['phone'];
$emails[] = $contact['email'];
}
}
var_dump($data);
and in result you'll get:
array(3) {
[0] =>
array(3) {
'name' =>
string(6) "name 1"
'phone' =>
string(8) "12341234"
'email' =>
string(12) "test@web.com"
}
[2] =>
array(3) {
'name' =>
string(6) "name 3"
'phone' =>
string(7) "4322342"
'email' =>
string(13) "test@web1.com"
}
[4] =>
array(3) {
'name' =>
string(6) "name 5"
'phone' =>
string(11) "12341266634"
'email' =>
string(18) "test@eqweqwweb.com"
}
}
this is just example.
How can I get the duplicate multidimensional array in php
// first : get all data, if the data same / duplicate take only one data
$unique = array_unique($data, SORT_REGULAR);
// then, get the data which duplicate with
$diffCellUniq = array_diff_key($data, $unique);
// so print the result
print_r($diffCellUniq); exit;
Array
(
[2] => Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
)
)
find duplicate value in multi-dimensional array
<?php
$array = array(array(7,18), array(12,7), array(12, 7, 13));
$result = array();
$first = $array[0];
for($i=1; $i<count($array); $i++){
$result = array_intersect ($first, $array[$i]);
$first = $result;
}
print_r($result);//7
?>
How to check if duplicate values of some indexes exist in multi dimensional array or not?
I achieved above scenario like this:
Dont know which one is best mine or other's who posted answers.
foreach($data as $key => $row)
{
$combinedarr[] = array("name"=>$row["name"],"type"=>$row["type"]);
}
//chck if same facilitiy is being visit on same date twice
$countcomb = count($combinedarr);
$uniquearr = array_unique($combinedarr, SORT_REGULAR);
if($countcomb==count($uniquearr)){
}else{
//yes duplicate exists
};
Thanks again for those who answered.
How to remove duplicate values from a multi-dimensional array in PHP
Here is another way. No intermediate variables are saved.
We used this to de-duplicate results from a variety of overlapping queries.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Finding duplicate values in multidimensional array
$output = array();
// loop through each user
foreach ($users as $id => $ips) {
// loop through each IP address
foreach ($ips as $ip) {
// add IP address, if not present
if (!isset($output[$ip])) {
$output[$ip] = array();
}
// add user ID to the IP address' array
$output[$ip][] = $id;
}
}
PHP How to count duplicate values in 2D array?
You can use array_count_values and array_column.
$counts = array_count_values(array_column($arr, "usr_name"));
This should give you an associative array with key being the name and the value being the count of that name.
Related Topics
Zf2 - Get Controller Name into Layout/Views
Validating Us Phone Number with PHP/Regex
PHP Recursively Unset Array Keys If Match
How Does PHP Compare Strings with Comparison Operators
Android Post Base64 String to PHP
What Is the Type This String? A:1:{S:2:"En";}
How to Implement Ocr on a Website Using PHP
Facebook Error 'Error Validating Verification Code'
Utc Date/Time String to Timezone
Retrieve Data from Db and Display It in Table in PHP .. See This Code Whats Wrong with It
How to Perform an Action Every 5 Results
Fix Incorrectly Displayed Encoding on an HTML Document with PHP
MySQL Error When Inserting Data Containing Apostrophes (Single Quotes)