我有两个数组
首先是

Array
(
    [0] => Array
        (
            [image] => Copy1vs.jpg
            [title] => V.S Achuthanandhan
            [groupId] => 1
            [masterId] => 1
            [id] => 1
            [itemId] => 1
            [status] => 1

        )

    [1] => Array
        (
            [image] => Copy1pinarayi.jpg
            [title] => Pinarayi Vijayan
            [groupId] => 1
            [masterId] => 2
            [id] => 2
            [itemId] => 2
            [status] => 1

        )

    [2] => Array
        (
            [image] => Copy1chandy.jpg
            [title] => Oommen Chandy
            [groupId] => 1
            [masterId] => 3
            [id] => 3
            [itemId] => 3
            [status] => 1

        )
)


其次是

Array
(
    [0] => Array
        (
            [image] => Copy1antony.jpg
            [title] => A. K. Antony
            [groupId] => 1
            [masterId] => 4
            [id] => 4
            [itemId] => 4
            [status] => 1

        )

)


我如何将这两个数组合并为一个数组
喜欢

Array
(
    [0] => Array
        (
            [image] => Copy1vs.jpg
            [title] => V.S Achuthanandhan
            [groupId] => 1
            [masterId] => 1
            [id] => 1
            [itemId] => 1
            [status] => 1

        )

    [1] => Array
        (
            [image] => Copy1pinarayi.jpg
            [title] => Pinarayi Vijayan
            [groupId] => 1
            [masterId] => 2
            [id] => 2
            [itemId] => 2
            [status] => 1

        )

    [2] => Array
        (
            [image] => Copy1chandy.jpg
            [title] => Oommen Chandy
            [groupId] => 1
            [masterId] => 3
            [id] => 3
            [itemId] => 3
            [status] => 1

        )

    [3] => Array
        (
            [image] => Copy1antony.jpg
            [title] => A. K. Antony
            [groupId] => 1
            [masterId] => 4
            [id] => 4
            [itemId] => 4
            [status] => 1

        )
)


我尝试了array_merge方法,但没有按照我的要求工作,是否可以不使用for循环..?
这些数组从数据库获取为

$itemListArray = array();
foreach($subcat as $sc){
                $itemList = DB::table('votemasteritems')
                        ->leftjoin('votemaster','votemaster.id','=','votemasteritems.masterId')
                        ->leftjoin('items','items.id','=','votemasteritems.itemId')
                        ->leftjoin('category','category.id','=','items.categoryId')
                        ->select('items.image','votemaster.title','votemaster.groupId','votemaster.id as masterId','votemasteritems.*')
                        ->where('votemaster.groupId',1)
                        ->where('category.Id',$sc->id)
                        ->get();

                array_merge($itemListArray, $itemList);

            }

最佳答案

array_merge不会修改您传递给它的数组。它只会返回包含所有值的新数组,因此在您的示例中,您将必须替换原始数组,例如:

$itemListArray = array_merge($itemListArray, $itemList);

10-07 19:38
查看更多