0

I have arrays generated from foreach loop with records from DB that I want to merge if email or iduser are identical. It looks like this

Array
(
    [iduser] => 1
    [email] => email1@example.com
    [firstname] => Jan
    [lastname] => Novotny
    [phone] =>
    [created] => 2011-10-28 16:53:33
    [last_action] => 2016-08-17 12:53:08
    [idclient] => 1
    [vat_type] => quarterly
    [eet] => 0
    [accounting_type] => accountancy
)

Array
(
    [iduser] => 1
    [email] => email1@example.com
    [firstname] => Jan
    [lastname] => Novotny
    [phone] =>
    [created] => 2013-11-12 18:09:11
    [last_action] => 2017-01-12 10:30:38
    [idclient] => 3
    [vat_type] => monthly
    [eet] => 1
    [accounting_type] => tax_registration
)
Array
(
    [iduser] => 7
    [email] => email2@example.com
    [firstname] => Honza
    [lastname] => Novak
    [phone] =>
    [created] => 2015-10-28 16:53:33
    [last_action] => 2010-01-27 02:18:09
    [idclient] => 336
    [vat_type] => quarterly
    [eet] => 0
    [accounting_type] => tax_registration
)

desired output is something like this.

Array
(
     [iduser] => 1
     [email] => email1@example.com
     [firstname] => Jan
     [lastname] => Novotny
     [phone] =>
     [created] => array(
                       [0]=> 2011-10-28 16:53:33
                       [1]=> 2013-11-12 18:09:11
     [last_action] => array(
                           [0]=> 2016-08-17 12:53:08
                           [1]=> 2017-01-12 10:30:38
     [idclient] => array(
                        [0]=> 1
                        [1]=> 3
     [vat_type] => array(
                        [0]=> quarterly
                        [1]=> monthly
     [eet] => array(
                   [0]=> 0
                   [1]=> 1
     [accounting_type] => array(
                               [0]=> accountancy
                               [1]=> tax_registration
)
Array
(
     [iduser] => 7
     [email] => email2@example.com
     [firstname] => Honza
     [lastname] => Novak
     [phone] =>
     [created] => 2015-10-28 16:53:33
     [last_action] => 2010-01-27 02:18:09
     [idclient] => 336
     [vat_type] => quarterly
     [eet] => 0
     [accounting_type] => tax_registration
)
..... etc

I tried to do it by array_merge() and array_replace_recursive() but couldn't come up with solution that works and didn't find answer in already asked questions on stackoverflow. I will appreciate any help :)

  • Have you seen [this SO question](http://stackoverflow.com/questions/14842272/php-array-merge-two-arrays-on-same-key) – Rüzgar May 21 '17 at 17:25
  • @DanielRahman Your concept is not optimal. You should choose just one "unique identifier" to merge on. By choosing two identifiers, the code will have to "decide" which `iduser` and which `email` it should be preserving and that isn't great. Since `email` is going to be unique per user, I strongly urge you to re-consider your concept/question and compare subarrays on `email`. Then do a quick SO search for your new question, because you will find page after page of answers for this. Your question is duplicated almost daily on SO. – mickmackusa May 21 '17 at 22:05

0 Answers0