Re: Unique items in an array
| From: | Marc Guay | Date: | Wed, 21 Dec 2011 19:05:17 +0000 |
| Subject: | Re: Unique items in an array | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316080@lists.php.net to get a copy of this message | ||
Hi folks,
I just wanted to add a little something to this thread as I reworked
some of the code and came across an interesting tidbit. I was trying
to merge many result sets which now had the table's primary key as the
array key, but found that when array_merge() was done, all of the
array's keys were lost, like so:
array(2) {
[704]=>
array(36) {
["contact_id"]=>
string(3) "704"
["contact_first_name"]=>
string(4) "Marc"
["contact_last_name"]=>
string(4) "Guay"
}
[705]=>
array(36) {
["contact_id"]=>
string(3) "705"
["contact_first_name"]=>
string(4) "Marc"
["contact_last_name"]=>
string(5) "Guay2"
}
}
array_merge with:
array(0) {
}
left me with:
array(2) {
[0]=>
array(36) {
["contact_id"]=>
string(3) "704"
["contact_first_name"]=>
string(4) "Marc"
["contact_last_name"]=>
string(4) "Guay"
}
[1]=>
array(36) {
["contact_id"]=>
string(3) "705"
["contact_first_name"]=>
string(4) "Marc"
["contact_last_name"]=>
string(5) "Guay2"
}
}
Pretty crummy. Apparently what I wanted was as simple as: $new_array
= $array1 + $array2, which I didn't even know was an option. Thank
you php.net/manual.
Marc