合并或组合关联数组子项

时间:2012-01-06 19:08:08

标签: php arrays multidimensional-array merge

我有一个想要合并(合并)的关联数组。实际代码是:

Array
(
    [0] => Array
        (
            [Id_Usager] => 291
            [etat_lundi] => livree
            [Date_lundi] => 2011-12-26
            [etat_mardi] => livree
            [Date_mardi] => 2011-12-27
            [etat_mercredi] => livree
            [Date_mercredi] => 2011-12-28
            [etat_jeudi] => livree
            [Date_jeudi] => 2011-12-29
            [etat_vendredi] => livree
            [Date_vendredi] => 2011-12-30
            [etat_samedi] => livree
            [Date_samedi] => 2011-12-31
            [etat_dimanche] => livree
            [Date_dimanche] => 2012-01-01
        )

    [1] => Array
        (
            [Id_Usager] => 291
            [etat_lundi] => livree
            [Date_lundi] => 2012-01-02
            [etat_mardi] => livree
            [Date_mardi] => 2012-01-03
            [etat_mercredi] => en_cours
            [Date_mercredi] => 2012-01-04
            [etat_jeudi] => en_cours
            [Date_jeudi] => 2012-01-05
            [etat_vendredi] => en_cours
            [Date_vendredi] => 2012-01-06
            [etat_samedi] => en_cours
            [Date_samedi] => 2012-01-07
            [etat_dimanche] => en_cours
            [Date_dimanche] => 2012-01-08
        )

)

我最终想要的是:

  1. 取消设置第一个元素(Id_Usager
  2. 将子阵列合并为一个
  3. 通过将etat值([etat_lundi] => livree)移动为Date键([Date_lundi] => 2012-01-02
  4. 来比较对

    我想要的结果是:

    Array(
        [0] => Array(
            [livree] => 2011-12-26
            [livree] => 2011-12-27
            [livree] => 2011-12-28
            [livree] => 2011-12-29
            [livree] => 2011-12-30
            [livree] => 2011-12-31
            [livree] => 2012-01-01
            [livree] => 2012-01-02
            [livree] => 2012-01-03
            [en_cours] => 2012-01-04
            [en_cours] => 2012-01-05
            [en_cours] => 2012-01-06
            [en_cours] => 2012-01-07
            [en_cours] => 2012-01-08
        )
    )
    

    我该怎么做?

1 个答案:

答案 0 :(得分:2)

array_merge()

在你的情况下:

$new_data = array_merge($data[0], $data[1]);

请注意,根据文档:

  

如果输入数组具有相同的字符串键,则后面的值   该密钥将覆盖前一个密钥。

从您的示例数据中可以看出,您有重复的键名称( Ben D 指出这是不可能的)。但是,它们包含相同的值。所以你应该没问题。