使用key将数组追加到多维数组中

时间:2015-07-09 16:19:37

标签: php arrays multidimensional-array

我有两个多维数组,我需要通过特定键合并。我尝试使用array_merge_recursive和array_search但效果不佳。

所以,

主阵列:

Array
(
    [group] => Array
        (
            [subgroup] => Array
                (
                    [items] => Array
                        (
                            [item 1] => web development
                            [item 2] => dba
                            [item 3] => qa
                        )
                )
        )
)

/* ------------ */

Array (
        [item 1] =>  Array
            (
                [User 1] => Array
                    (
                        [properties] => Array
                            (
                                [id] => conexion_1624
                                [name] => Jhon Doe 
                            )
                    )
                [User 2] => Array
                    (
                        [properties] => Array
                            (
                                [id] => conexion_2001
                                [name] => Alice and Bob
                            )
                    )
            )
    )

预期结果:

Array
(
    [group] => Array
        (
            [subgroup] => Array
                (
                    [items] => Array
                        (
                            [item 1] => Array
                                (
                                    [User 1] => Array
                                        (
                                            [properties] => Array
                                                (
                                                    [id] => conexion_1624
                                                    [name] => Jhon Doe  
                                                )
                                        )
                                    [User 2] => Array
                                        (
                                            [properties] => Array
                                                (
                                                    [id] => conexion_2001
                                                    [name] => Alice and Bob
                                                )
                                        )
                                )
                            [item 2] => dba
                            [item 3] => qa
                        )
                )
        )
)

2 个答案:

答案 0 :(得分:1)

如果数组结构未更改

foreach($the2nd as $key => $item) 
  $the1st['group']['subgroup']['items'][$key] = $item;
由于在评论中讨论了

更新

如果第一个数组中的项目可能不在同一级别,则此类代码将找到它们

array_walk_recursive ( 
     $the1nd , 
     function($v, $k, $the2nd) { if(isset($the2nd[$k])) $v = $the2nd[$k]; },
     $the2nd);

答案 1 :(得分:0)

以下代码可以为您提供所需的结果。

class streams: Object {
    dynamic var br = ""
    dynamic var categorie = 0
    dynamic var ct = ""
    dynamic var lc = ""
    dynamic var ml = ""
    dynamic var mt = ""
    dynamic var name = ""
    dynamic var shoutcatid = 0
    dynamic var stationid = 0

    override static func primaryKey() -> String? {
        return "stationid"
    }
}