0

I have two arrays First is

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

        )
)

And Second is

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

        )

)

How can i combine these two arrays as a single array like

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

        )
)

i tried array_merge method but not working as per my requirement is it possible without using a for loop..? these arrays gets from databse as

$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); 

            }
2
  • 1
    How about a good ol' fashioned array_merge() ? secure.php.net/manual/en/function.array-merge.php Commented Nov 1, 2015 at 9:30
  • I edited my post. array merge method is not working. Both the array have same name . Commented Nov 1, 2015 at 9:33

4 Answers 4

3

Yes, PHP has the array_merge() function: http://php.net/array_merge

Use it like $combinedArray = array_merge($array1, $array2);.

Sign up to request clarification or add additional context in comments.

1 Comment

i already try that but array merge is not working.i just edited the question.please see
1

array_merge doesn’t modify the arrays you pass to it. It only returns a new array containing all of the values, so in your example you’d have to replace the original array like:

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

Comments

0

check this one

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();

            $itemListArray[]=$itemList; 

        }

Comments

0

Try array_merge() function

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

In your case, it should be:

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

2 Comments

i have both arrays having same name
@Vishal In your case, shouldn't it be: $itemListArray = array_merge($itemListArray, $itemList);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.