note 91134 added to function.array-unique
| From: | steve at visual77 dot com | Date: | Tue, 26 May 2009 23:41:48 +0000 |
| Subject: | note 91134 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156038@lists.php.net to get a copy of this message | ||
One way to use array_unique() to handle the multidimensional array issue is to use an md5 hash of
the serialized array as the key. I've seen some solutions listed here that stored the
serialized string, but that requires unserialization, and moving data back and forth between two
states for little reason is just a bad idea. Instead, you could try this method:
<code>
$values = array();
foreach($data as $d) {
$values[md5(serialize($d))] = $d;
}
sort($values);
</code>
This will serialize and hash the value, and store the value with the hash as they key. This will
ensure that each piece is only stored once, as the second attempt to store will just overwrite the
first attempt with identical data.
The sort() at the end is a bit unnecessary, but I had to use it on my project, because the function
I was passing the array to couldn't handle keys that weren't valid XML node names (and
some md5 hash strings will start with numbers, which is invalid for XML).
----
Server IP: 208.69.120.35
Probable Submitter: 24.234.114.199
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/91134
Del: integrated -- https://master.php.net/note/delete/91134/integrated
Del: useless -- https://master.php.net/note/delete/91134/useless
Del: bad code -- https://master.php.net/note/delete/91134/bad+code
Del: spam -- https://master.php.net/note/delete/91134/spam
Del: non-english -- https://master.php.net/note/delete/91134/non-english
Del: in docs -- https://master.php.net/note/delete/91134/in+docs
Del: other reasons-- https://master.php.net/note/delete/91134
Reject -- https://master.php.net/note/reject/91134
Search -- https://master.php.net/manage/user-notes.php