Merge first array particular key and value into second array

php merge array values with same keys
php merge multidimensional array by key value
how to merge two array with same keys without loop in php
php combine array keys and values
php merge multidimensional array into one
php merge array with same values
php array same key different value
php merge associative arrays by key

I have 2 array

$arr1 = Array ( 
    [0] => Array ( [customer_id] => 1 [Expire] => 2019-05-14 [paid] => 1 ) 
    [1] => Array ( [customer_id] => 2 [Expire] => 2019-06-20 [paid] => 0 ))

and

$arr2 = Array ( 
    [0] => Array ( [id] => 3943 [customer_id] => 1 [Expire] => 2019-05-14 ) 
    [1] => Array ( [id] => 3944 [customer_id] => 1[Expire] => 2019-05-14 ) 
    [2] => Array ( [id] => 4713 [customer_id] => 2 [Expire] => 2019-06-20 ) 
)

and try to put first array key and value [paid]=>1 or 0 in second array if customer id and expire match like

Array ( 
    [0] => Array ( [id] => 3943 [customer_id] => 1 [Expire] => 2019-05-14 [paid] => 1) 
    [1] => Array ( [id] => 3944 [customer_id] => 1 [Expire] => 2019-05-14 [paid] => 1) 
    [2] => Array ( [id] => 4713 [customer_id] => 2 [Expire] => 2019-06-20 [paid] => 0) 
)

I try to merge array in php but not get exact what i want. Is there any php function to do it?.

array_combine - Manual, array_combine — Creates an array by using one array for keys and another for its values Creates an array by using the values from the keys array as keys and the values from the values array as the in order to preserve duplicate keys when combining arrays. fewer elements in the first array, add extra keys for($i = 0� array_merge ([ array $ ]) : array Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

sometimes things can be done in hard way:)

<?php
$arr1 = [
    ['customer_id' => 1, 'Expire' => '2019-05-14', 'paid' => 1],
    ['customer_id' => 2, 'Expire' => '2019-06-20', 'paid' => 0],
];

$arr2 = [
    ['id' => 3943, 'customer_id' => 1, 'Expire' => '2019-05-14'],
    ['id' => 3944, 'customer_id' => 1, 'Expire' => '2019-05-14'],
    ['id' => 3945, 'customer_id' => 2, 'Expire' => '2019-05-14'],
    ['id' => 4713, 'customer_id' => 2, 'Expire' => '2019-06-20'],
];

foreach ($arr2 as &$item2) {
    foreach ($arr1 as $item1) {
        if (
            $item2['customer_id'] === $item1['customer_id']
            && $item2['Expire'] === $item1['Expire']
        ) {
            $item2['paid'] = $item1['paid'];
            break;
        }
    }
}
unset($item2);

var_dump($arr2);

How to merge the first index of an array with the first index of second , How to merge the first index of an array with the first index of second array? Last Updated: array ();. foreach ( $array1 as $key => $value ){. ANALYSIS. In this C merge two arrays example, below For loop will help to iterate every cell present in a[3] array. Condition inside the for loops (i < Size) will ensure the compiler, not to exceed the array limit.

If you cannot change the layout of the first array I think it will be best to first create an intermediate array that keeps a record of all expire dates for every customer.

The following implementation does not require you to use a nested loop.

<?php
$arr1 = [
    ['customer_id' => 1, 'Expire' => '2019-05-14', 'paid' => 1],
    ['customer_id' => 2, 'Expire' => '2019-06-20', 'paid' => 0],
];

$arr2 = [
    ['id' => 3943, 'customer_id' => 1, 'Expire' => '2019-05-14'],
    ['id' => 3944, 'customer_id' => 1, 'Expire' => '2019-05-14'],
    ['id' => 3945, 'customer_id' => 2, 'Expire' => '2019-05-14'],
    ['id' => 4713, 'customer_id' => 2, 'Expire' => '2019-06-20'],
];

// Create a list of all paid, expiry dates, keyed by customer_id
$payed = [];

foreach ($arr1 as $item) {
    if (!isset($payed[$item['customer_id']])) {
        $payed[$item['customer_id']] = [];
    }
    $payed[$item['customer_id']][] = $item['Expire'];
}

// Lookup the customer and expire date for every entry
$arr2 = array_map(function($item) use ($payed) { 
    $item['paid'] = in_array($item['Expire'], $payed[$item['customer_id']] ?? []);
    return $item;
}, $arr2);

Result:

Array
(
[0] => Array
    (
        [id] => 3943
        [customer_id] => 1
        [Expire] => 2019-05-14
        [paid] => 1
    )

[1] => Array
    (
        [id] => 3944
        [customer_id] => 1
        [Expire] => 2019-05-14
        [paid] => 1
    )

[2] => Array
    (
        [id] => 3945
        [customer_id] => 2
        [Expire] => 2019-05-14
        [paid] => 
    )

[3] => Array
    (
        [id] => 4713
        [customer_id] => 2
        [Expire] => 2019-06-20
        [paid] => 1
    )

)

See demo

Merge an array of size n into another array of size m+n , Merge these two arrays into the first array of size m+n such that the output is sorted. NA => Value is not filled/available in array mPlusN[]. And I want to merge them on the same key AND value. If they have the same ur_user_id then they are merged. array2 only provides some additional data for array1, so the new_array.length = array1.length. array1 just gets the additional data from array2. $array1 = array( array('ur_user_id'=> 1,'ur_fname'=>'PerA','ur_lname'=>'SonA'), array('ur_user_id'=> 2,'ur_fname'=>'PerB','ur_lname'=>'SonB'), array('ur_user_id'=> 3,'ur_fname'=>'PerC','ur_lname'=>'SonC'), ); $array2 = array( array('ur_user_id

This can fix the issue :

$arr1 = array(

            ["customer_id"=>1,"Expire"=> "2019-05-14", "paid"=>1],
            ["customer_id"=>2,"Expire"=> "2019-06-20", "paid"=>0]
    );


$arr2 = array(
           ["id"=>3943, "customer_id"=>1,"Expire"=> "2019-05-14"],
           ["id"=>3944,"customer_id"=>2,"Expire"=> "2019-06-20"],
           ["id"=>4713,"customer_id"=>1,"Expire"=> "2019-05-14"]
    );    


$result= array();    

function getRowByCustomerID($id, $array){
    foreach($array as $value){
        if($value['customer_id'] ==$id){
            return $value;
        }
    }
    return null;
}    

foreach($arr2 as $subarr){

    $object = getRowByCustomerID($subarr['customer_id'],$arr1 );
    if(!is_null($object)){
        $object['id']=$subarr['id'];
        $result[]= $object;
    }
}    



var_dump($result);

the output is similar to what you are looking for :

array(3) {
  [0]=>
  array(4) {
    ["customer_id"]=>
    int(1)
    ["Expire"]=>
    string(10) "2019-05-14"
    ["paid"]=>
    int(1)
    ["id"]=>
    int(3943)
  }
  [1]=>
  array(4) {
    ["customer_id"]=>
    int(2)
    ["Expire"]=>
    string(10) "2019-06-20"
    ["paid"]=>
    int(0)
    ["id"]=>
    int(3944)
  }
  [2]=>
  array(4) {
    ["customer_id"]=>
    int(1)
    ["Expire"]=>
    string(10) "2019-05-14"
    ["paid"]=>
    int(1)
    ["id"]=>
    int(4713)
  }
}

PHP array_merge() Function, Merge two arrays into one array: Note: If you assign only one array to the array_merge() function, and the keys are integers, the function Instead of override the keys, the array_merge_recursive() function makes the value as an array. Merge keys array and values array into an object in JavaScript. the second is known as currying and partial application. parallel arrays of key-value pairs.

jQuery.merge(), The second array-like object to merge into the first, unaltered. The $.merge() operation forms an array that contains all elements from the two arrays. The orders� Does pretty much the same as the solution above it just have more practical use case. This function doesn't concatenate duplicate values in to one array only so that it can delete them at some later stage. SHORT FUNCTION DEFINITION ( only 9 lines ) /** * This function merging only arrays unique values.

2 Ways to Merge Arrays in JavaScript, Here are 2 ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need older browser support, you should use Concat. Module array-merge-by-key exports function mergeByKey (key, array1, array2,) by default. Module uses Object.assign to merge two objects having the same key (property) value. The assignment is performed from right to left, which means that the most right passed array will have priority over previous ones.

Union of objects in javaScript based on unique value, Merging of two or more objects; Merging of object into an Array; Merging two on unique key; Union of objects in an array based on unique key value of second array object, but it won't get the property from first array object. array_combine(array$keys, array$values) : array Creates an arrayby using the values from the keysarray as keys and the values from the valuesarray as the corresponding values.

Comments
  • What happened to the id 3945, customer_id 2? Why isn't that present in the final desired output?
  • Can you make the index of the array as the customer_id instead of 0,1,2..etc? It will make things much easier
  • customer_id value can't be 0
  • It is best to show us what you've tried and say where it goes wrong. @Qirel The offending id was removed by Deepak3301086 five minutes ago.
  • This is, of course, a good solution. Just want to point out you could have done it the other way around: foreach ($arr1 as and search through a column of $arr2. Why? Because it seems that $arr1 is the incoming payment information and $arr2 is an existing list of customers. I also would put array_column($arr1, 'customer_id') outside the foreach loop. But hey, these are minor details, this is by far the best solution. You get my upvote.
  • Hmm, that may very well be. The issue is though that there are multiple instances of each customer ID in the second array, but only one in the first. There's also fewer entries in the first array than the second. Which means it'll be a tad more tricky to get the desired result (meaning you can't use the snippet above an just replace $arr1 by $arr2 and vice-versa). You're probably right about the array-definitions though.
  • Yeah, I just realized that. That forces you to do it this way. It would be better if the arrays, in the question, had proper names, like $payments, $customers, or $orders, that would give you an better idea what you're dealing with.
  • Thanks, It's work for me. It save second loop time.
  • It's only work for customer id match, if date are different then it's not work. Please check 3v4l.org/TeTpK
  • I think that the === triple equals 'Identical Operator' is over the top here. The == double equals 'Equal Operator' will do. I really wouldn't care if an 'id' was 3943 (integer) or "3943" (string), moreso, I would think they are equal and should be seen as such.
  • Your $arr2 is not the same as in the question. See the 'paid' keys.
  • you're right, i jut update my answer , this will not hurt the result . thank you @KIKOSoftware