note 89729 added to function.array-unique
| From: | kadbleghnet at osu1 dot php dot net | Date: | Fri, 20 Mar 2009 17:49:24 +0000 |
| Subject: | note 89729 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152215@lists.php.net to get a copy of this message | ||
florian at box dot net is right, having an array of arrays does not work right out of the box, but
using json makes it hard for previous php versions to work. Using a simple serialize is very easy:
<?php
$parent = array();
$son1 = array('a' => 1, 'b' => 2);
$son2 = array('c' => 3, 'd' => 4);
$son3 = array('a' => 5, 'd' => 4);
// Fill our array, doesn't matter how, but serialize your input
for ($j = 0; $j < 6; $j++){
$parent[] = serialize($son1);
$parent[] = serialize($son2);
$parent[] = serialize($son3);
}
var_dump($parent);
var_dump(array_unique($parent));
?>
This will print:
array(18) {
[0]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[1]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[2]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
[3]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[4]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[5]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
[6]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[7]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[8]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
[9]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[10]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[11]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
[12]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[13]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[14]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
[15]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[16]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[17]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
}
array(3) {
[0]=>
string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}"
[1]=>
string(30) "a:2:{s:1:"c";i:3;s:1:"d";i:4;}"
[2]=>
string(30) "a:2:{s:1:"a";i:5;s:1:"d";i:4;}"
}
And just unserialize it :)
NOTE: Of course (un)serializing large amounts of data will bring the performance problems
associated, so is up to you to decide.
----
Server IP: 69.147.83.197
Probable Submitter: 201.144.39.83
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/89729
Del: integrated -- https://master.php.net/note/delete/89729/integrated
Del: useless -- https://master.php.net/note/delete/89729/useless
Del: bad code -- https://master.php.net/note/delete/89729/bad+code
Del: spam -- https://master.php.net/note/delete/89729/spam
Del: non-english -- https://master.php.net/note/delete/89729/non-english
Del: in docs -- https://master.php.net/note/delete/89729/in+docs
Del: other reasons-- https://master.php.net/note/delete/89729
Reject -- https://master.php.net/note/reject/89729
Search -- https://master.php.net/manage/user-notes.php