将第一个数组的特定键和值合并到第二个数组中

时间:2019-04-09 08:32:16

标签: php arrays

我有2个数组

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

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

并尝试将第一个数组键和值[paid] = >> 1或0放入第二个数组中,如果客户ID和过期匹配如

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

我尝试在php中合并数组,但未获得我想要的确切值。有任何php函数可以做到吗?。

4 个答案:

答案 0 :(得分:3)

循环第二个数组$arr2,找到第一个数组$arr1的索引,其中列customer_id$arr2的当前迭代相同。 array_search()返回该索引,然后我们可以使用它来获取该数组的paid索引。然后,通过执行$arr2,将其附加到数组$a['paid'] = $paid;中。

由于我们通过引用循环了数组 (循环中&之前的$a),因此我们对其所做的更改将被应用回原始数组

foreach ($arr2 as &$a) {
    $customerID = $a['customer_id'];      // This customer ID
    $arr1_key = array_search($customerID, array_column($arr1, 'customer_id'));   // Find the index of this customerID in the first array
    $paid = $arr1[$arr1_key]['paid'];     // Find the paid value matching that customer ID
    $a['paid'] = $paid;
}

更新
如果需要它与ID和到期日期匹配,请使用array_filter()来获取$arr1中与日期和ID匹配的数组元素。使用reset(),我们使用该数组中的第一个元素。

foreach ($arr2 as &$a) {
    // Find the sub-array of $arr1 that matches this array's date and ID
    $arr1_match  = array_filter($arr1, function($v) use ($a) {
        return $v['customer_id'] == $a['customer_id'] && $v['Expire'] == $a['Expire'];
    });
    // Get the first element from the result
    $paid = reset($arr1_match)['paid'];
    // Append the paid-value to this array (done by reference)
    $a['paid'] = $paid;
}

答案 1 :(得分:1)

有时候事情可以用艰难的方式完成:)

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

答案 2 :(得分:1)

这可以解决问题:

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

输出类似于您正在寻找的内容:

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)
  }
}

答案 3 :(得分:1)

如果您不能更改第一个数组的布局,我认为最好首先创建一个中间数组,该数组记录每个客户的所有到期日期。

以下实现不需要您使用嵌套循环。

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

结果:

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