note 89729 deleted from function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 15:23:17 +0000 |
| Subject: | note 89729 deleted from function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170458@lists.php.net to get a copy of this message | ||
Note Submitter: kad blegh net
----
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.