-1

I am trying to Merge same column in two different array, Array2 is the part of Array1, for some updation on PartyName column I am fetching that column from main array1 using array_column then applying some modification to array2 then want to merge it again. like

Array1:

Array
(
    [0] => Array
        (
            [StorePartyId] => 10462791
            [StoreId] => 4
            [PartyName] => AMAR MEDICO
            [PartyCode] => 6840
        )

    [1] => Array
        (
            [StorePartyId] => 10463839
            [StoreId] => 4
            [PartyName] => NEW SAVE MEDICINE SHOPEE
            [PartyCode] => 8236
        )
)

Array2:

Array
(
    [0] => Array
        (
            [PartyName] => AMAR MEDICO_updated
        )

    [1] => Array
        (
            [PartyName] => NEW SAVE MEDICINE SHOPEE_updated
        )
)

I can programmatically merge this two array by looping each other. but looking for some inbuilt function of php array. I tried array_merge function but it does not resolve this.

FinalArray

final array should look like this

Array
(
    [0] => Array
        (
            [StorePartyId] => 10462791
            [StoreId] => 4
            [PartyName] => AMAR MEDICO_updated
            [PartyCode] => 6840
        )

    [1] => Array
        (
            [StorePartyId] => 10463839
            [StoreId] => 4
            [PartyName] => NEW SAVE MEDICINE SHOPEE_updated
            [PartyCode] => 8236
        )
)
1

3 Answers 3

3

Use array_replace_recursive to replace recursive arrays.
array_replace_recursive($Main_array, $Replacements);

$new = array_replace_recursive($arr,$arr2);

Returns:

array(2) {
  [0]=>
  array(4) {
    ["StorePartyId"]=>
    int(10462791)
    ["StoreId"]=>
    int(4)
    ["PartyName"]=>
    string(19) "AMAR MEDICO_updated"
    ["PartyCode"]=>
    int(6840)
  }
  [1]=>
  array(4) {
    ["StorePartyId"]=>
    int(10463839)
    ["StoreId"]=>
    int(4)
    ["PartyName"]=>
    string(32) "NEW SAVE MEDICINE SHOPEE_updated"
    ["PartyCode"]=>
    int(8236)
  }
}

https://3v4l.org/fjTlq

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

Comments

0

Lets assume

Your current data is stored in $array1, updated field data is stored in $array2

foreach($array1 as $key=>&$arr){
    if(isset($array2[$key])){
        $arr=array_merge($arr,$array2[$key]);
    }
}

Here I have used & sign for pass reference of the element. Now $array1 is final array with updated value

Comments

-1

I use array_merge and it works as you wish

$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));

Result :

Array ( [a] => red [b] => yellow [c] => blue )

1 Comment

Won't work in this case 3v4l.org/qblkZ

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.