Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于键的值合并数组_Php_Arrays - Fatal编程技术网

Php 基于键的值合并数组

Php 基于键的值合并数组,php,arrays,Php,Arrays,我有从foreach循环生成的数组,其中包含来自DB的记录,如果email或iduser相同,我希望合并这些记录。看起来像这样 Array ( [iduser] => 1 [email] => email1@example.com [firstname] => Jan [lastname] => Novotny [phone] => [created] => 2011-10-28 16:53:33 [la

我有从foreach循环生成的数组,其中包含来自DB的记录,如果email或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\u merge()和array\u replace\u recursive()来实现这一点,但没有找到有效的解决方案,也没有在有关stackoverflow的问题中找到答案。我将感谢您的帮助:)

您有没有看到@DanielRahman您的概念不是最佳的。您应该只选择一个“唯一标识符”进行合并。通过选择两个标识符,代码必须“决定”应该保留哪个
iduser
和哪个
email
,这并不好。由于
电子邮件
对于每个用户都是唯一的,我强烈建议您重新考虑您的概念/问题,并比较
电子邮件
上的子数组。然后快速搜索你的新问题,因为你会找到一页又一页的答案。你的问题几乎每天都被重复。