note 125828 added to function.array-unique
| From: | contact at evoweb dot fr | Date: | Tue, 16 Feb 2021 08:32:25 +0000 |
| Subject: | note 125828 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219133@lists.php.net to get a copy of this message | ||
Here is a solution to make unique values keeping empty values for an array with keys :
<?php
function array_unique_kempty($array) {
$values = array_unique($array);
$return = array_combine(array_keys($array), array_fill(0,count($array),null));
return array_merge($return,$values);
}
$myArray = [
"test1" => "aaa",
"test2" => null,
"test3" => "aaa",
"test4" => "bbb",
"test5" => null,
"test6" => "ccc",
"test7" => "ddd",
"test8" => "ccc"
];
echo "<pre>".print_r(array_unique_kempty($myArray),true)."</pre>";
/*
Array
(
[test1] => aaa
[test2] =>
[test3] =>
[test4] => bbb
[test5] =>
[test6] => ccc
[test7] => ddd
[test8] =>
)
*/
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.245 (proxied: 176.157.206.1)
----
Manual Page -- http://php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/125828
Del: integrated -- https://master.php.net/note/delete/125828/integrated
Del: useless -- https://master.php.net/note/delete/125828/useless
Del: bad code -- https://master.php.net/note/delete/125828/bad+code
Del: spam -- https://master.php.net/note/delete/125828/spam
Del: non-english -- https://master.php.net/note/delete/125828/non-english
Del: in docs -- https://master.php.net/note/delete/125828/in+docs
Del: other reasons-- https://master.php.net/note/delete/125828
Reject -- https://master.php.net/note/reject/125828
Search -- https://master.php.net/manage/user-notes.php