根据键的值合并数组

时间:2017-05-21 17:16:45

标签: php arrays

我有从foreach循环生成的数组,其中包含来自数据库的记录,如果电子邮件或iduser相同,我想合并。看起来像这样

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
)

期望的输出是这样的。

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

我尝试通过array_merge()和array_replace_recursive()来完成它,但是无法提供有效的解决方案,并且在stackoverflow上的已经问过的问题中找不到答案。我将不胜感激任何帮助:)

0 个答案:

没有答案