0

对不起,把标题弄得听起来很复杂。

我希望将一个数组合并到一个已经创建的大型多维数组中。

第一个想法...是否可以将子数组创建为 array(); 同时循环您创建的数组?

例如

    foreach ($records as $record) {
        $record['AddedContent']['Comments'] = array(); //doesn't currently exist
            foreach ($comments as $comment) {
                if($record['AddedContent']['id'] = $comment['content_id']){
                    array_push($record['AddedContent']['Comments'], $comment);

                }
            }
        }

这里的评论是我想加入记录的小数组(大数组)

数组如下:

[0] => Array
    (
        [SubscribedToProfile] => Array
            (
                [id] => 
                [user_id] => 
                [company_name] => 
                [picture_filename] => 
            )

        [AddedContent] => Array
            (
                [text] => 
                [file_location] => 
                [content_type_id] => 4
                [file_name] => IxnAvLKMtIU
                [id] => 87
            )

    )

评论数组是:

Array
 (
   [0] => Array
    (
        [ContentComment] => Array
            (
                [id] => 3
                [content_id] => 87
                [text] => jhbuoijniknb

                [created] => 2013-10-21 13:08:12
                [user_id] => 2097
            )

    )

[1] => Array
    (
        [ContentComment] => Array
            (
                [id] => 4
                [content_id] => 88
                [text] => gfrgfrtegvrtegvrtgvrtvtrgv
                [created] => 2013-10-21 13:08:12
                [user_id] => 2097
            )
    )

)

我需要它是:

[0] => Array
    (
        [SubscribedToProfile] => Array
            (
                [id] => 
                [user_id] => 
                [company_name] => 
                [picture_filename] => 
            )

        [AddedContent] => Array
            (
                [text] => 
                [file_location] => 
                [content_type_id] => 4
                [file_name] => IxnAvLKMtIU
                [id] => 87
                [comments] => Array
                            (
                            [0] => Array
                                    (
                                     [id] => 2
                                     [content_id] => 87
                                     [text] => fderwcfrvfcerwfvrev
                                     [created] => 2013-10-21 13:05:58
                                     [user_id] => 2097
                                     )
                             [1] => Array
                                    (
                                     [id] => 3
                                     [content_id] => 87
                                     [text] => jhbuoijniknb
                                     [created] => 2013-10-21 13:08:12
                                     [user_id] => 2097
                                     )
                                )
                    )
            )

我需要循环遍历 bpth 数组,因为我需要有条件地加入 htem,看看 ID 为 87(在本例中)和内容 ID 为 87 的位置。

4

1 回答 1

0

您的解决方案就在这里。

foreach ($records as $key=>$record) {
    $array = array();
    if($key=='AddedContent') {
        foreach($comments as $comment) {
            if($record['id'] == $comment["ContentComment"]['content_id']){
                array_push($array, $comment["ContentComment"]);
            }
        }
        $records[$key]['comments'] = $array;
    }
}
于 2013-10-22T10:47:26.727 回答