数组合并NO(+)UNION或merge()

$array1 = [ "month" => "Jan", "count" => "20" , "month" => "feb", "count" => "2" ];
$array2 = [ "month" => "Jan", "count" => "50" , "month" => "feb", "count" => "27" ];

Expected array after merge the above 2 arrays

$fianlarray = ["month" => "jan" , "count"=>"20" , "count" => "50" , 
               "month" => "feb" , "count" => "2", "count" => "27" ];

i've tried array_merge and union but no result. thanks in advance

You cannot do such a thing :

$fianlarray = ["month" => "jan" , "count"=>"20" , "count" => "50" , 
               "month" => "feb" , "count" => "2", "count" => "27" ];

here you define multiple times the keys "month","count" which is forbidden...

I think there is issue in how you storing data. You cannot use same key twice inside array at same level, this will result in overwriting old data.

$array1 = [ "month" => "Jan", "count" => "20" , "month" => "feb", "count" => "2" ]; $array2 = [ "month" => "Jan", "count" => "50" , "month" => "feb", "count" => "27" ];

Here in your code you are storing 'month' as 'jan' then 'month' as 'feb', this is overwrite the previous assignment of 'month' => 'jan'.

Please restructure how you storing info inside array.

Refer this question. Nicely explained by amber.

If more help needed, I'm happy to help.

Thanks, Happy Coding.

Unfortunately, we can not do as you mentioned in your question, you can not use same associative key again, in this case repeated associative key overrides value

$fianlarray = ["month" => "jan" , "count"=>"20" , "count" => "50" , 
               "month" => "feb" , "count" => "2", "count" => "27" ];

I suggest you to do good structure like below

$fianlarray = [["month" => "jan" , "count"=>["20", "50"]], 
               ["month" => "feb" , "count" => ["2","27"]]];

OR

$fianlarray = ["jan"=>["20", "50"],"feb"=>["2","27"]];

@Aravindh Gopi You can not get array like you want because array can not have same name index more than one, if you will do this so array will overwrite the values and in this case $array1 values will be over written by $array2 value and your will get your final array like:

$fianlarray = ["month" => "feb", "count" => "27" ];

But if you want your solution so you can do it with the function array_merge_recursive(), this function will give you, your solution but you will get the same index values in an new array with 0 and 1 index try below one:

<?php
    $array1 = [ "month" => "Jan", "count" => "20"];
    $array2 = ["month" => "feb", "count" => "27" ];
    $fianlarray=array_merge_recursive($array1,$array2);
    print_r($fianlarray);
?>

OutPut will be:

Array ( [month] => Array ( [0] => Jan [1] => feb ) [count] => Array ( [0] => 20 [1] => 27 ) )

So try if your need is this (y)