note 128025 added to function.array-unique
| From: | MikeD dot -michalateuro-net dot pl at main dot php dot net | Date: | Sat, 17 Dec 2022 00:47:21 +0000 |
| Subject: | note 128025 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219981@lists.php.net to get a copy of this message | ||
modified code originally posted by Ghanshyam Katriya(anshkatriya at gmail) [highest voted comment
here].
1. In php 7.4 counter $i breaks the function. Removed completely (imo was waste of keystrokes
anyway).
2. I added second return value - array of duplicates. So you can take both and compare them (I had
to).
Example array (copy-paste from original post):
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike",
"num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa",
"num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew",
"num"=>"784581254"),
);
?>
Function:
<?php
function unique_multidim_array($array, $key) : array {
$uniq_array = array();
$dup_array = array();
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[] = $val[$key];
$uniq_array[] = $val;
/*
# 1st list to check:
# echo "ID or sth: " . $val['building_id'] . "; Something else:
" . $val['nodes_name'] . (...) "\n";
*/
} else {
$dup_array[] = $val;
/*
# 2nd list to check:
# echo "ID or sth: " . $val['building_id'] . "; Something else:
" . $val['nodes_name'] . (...) "\n";
*/
}
}
return array($uniq_array, $dup_array, /* $key_array */);
}
?>
Usage:
<?php
list($unique_addresses, $duplicates, /* $unique_keys */) =
unique_multidim_array($details,'id');
?>
Then:
var_dump($unique_addresses);
or
var_dump($duplicates);
or foreach or whatever. Personally I just echo-ed 1st and then 2nd (both DOUBLE COMMENTED) list in
function itself (then copied both to notepad++ and compared them - just to be 100% sure), but in
case you want to do something else with it - enjoy :)
Plus - as a bonus - you also get an array of UNIQUE keys you searched for (just uncomment
>$key_array< in both: function return and function call code).
From example array code returns:
var_dump($unique_addresses);
array(2) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["name"]=>
string(4) "Mike"
["num"]=>
string(10) "9876543210"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["name"]=>
string(7) "Carissa"
["num"]=>
string(11) "08548596258"
}
}
var_dump($duplicates);
array(1) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["name"]=>
string(6) "Mathew"
["num"]=>
string(9) "784581254"
}
}
Plus keys, if you want.
P.S.: in my - practical - case of DB querying I got around 4k uniques and 15k dupes :)
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.54 (proxied: 77.46.36.60)
----
Manual Page -- https://php.net/manual/en/function.array-unique.php
Edit -- https://main.php.net/note/edit/128025
Del: integrated -- https://main.php.net/note/delete/128025/integrated
Del: useless -- https://main.php.net/note/delete/128025/useless
Del: bad code -- https://main.php.net/note/delete/128025/bad+code
Del: spam -- https://main.php.net/note/delete/128025/spam
Del: non-english -- https://main.php.net/note/delete/128025/non-english
Del: in docs -- https://main.php.net/note/delete/128025/in+docs
Del: other reasons-- https://main.php.net/note/delete/128025
Reject -- https://main.php.net/note/reject/128025
Search -- https://main.php.net/manage/user-notes.php