0

Array 1

array(1) {
[0]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplist_name"]=>
        string(18) "PM_Mon_Wed_Fri_Sun"
        ["shoplist_status"]=>
        int(0)
    }
}

Array 2

array(3) {
[2184]=>
        array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplistshop_id"]=>
        int(7)
        ["shoplistshop_status"]=>
        int(0)
}
[2184]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplistshop_id"]=>
        int(8)
        ["shoplistshop_status"]=>
        int(0)
    }
}
[2185]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplistshop_id"]=>
        int(9)
        ["shoplistshop_status"]=>
        int(0)
    }
}

What I want

array(2) {
[0]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplist_name"]=>
        string(18) "PM_Mon_Wed_Fri_Sun"
        ["shoplist_status"]=>
        int(0)
        array(3) {
            [0]=>
                array(2) {
                    ["shoplistshop_id"]=>
                    int(7)
                    ["shoplistshop_status"]=>
                    int(0)
                }
            [1]=>
                array(2) {
                    ["shoplistshop_id"]=>
                    int(8)
                    ["shoplistshop_status"]=>
                    int(0)
                }
            [2]=>
                array(2) {
                    ["shoplistshop_id"]=>
                    int(9)
                    ["shoplistshop_status"]=>
                    int(0)
                }
        }
    }
}

Primary key is shoplist_id.

I need to merge array 1 and array 2 since they have both the same key and value.

I tried to use array_merge / array_merge_recursive seems like not working.

So how can I merge array 1 and 2 with the same key and same value? Is that anything I miss? Much thanks.

Ck Wong
  • 11
  • 3
  • Take a look at [this question](https://stackoverflow.com/questions/53938996/how-to-combine-2-arrays-that-have-the-same-index-and-value-with-php/53939075#53939075) your answer will be quite similar – Nick Dec 27 '18 at 03:30
  • 1
    Where do you get your Array 1 and Array 2 from? Your desired outcome is invalid syntax. Your subarray needs to have a key. – Sean Dec 27 '18 at 03:30

1 Answers1

0

You can use array_merge here:

$expectedOutputArray = array_merge($array1[0], $array2);

As you can see, $array1[0] itself an array and hence, it will merge and give expected output.

Ayush Jain
  • 337
  • 3
  • 10