Re: Unique items in an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux