简体   繁体   English

php 合并两个 arrays

[英]php merge two arrays

I have two arrays which i want to merge but following certain conditions.我有两个 arrays 我想合并但遵循某些条件。

$wt = [
    ['time' =>'07:00'],
    ['time' =>'07:30'],
    ['time' =>'08:00'],
    ['time' =>'08:30'],
    ['time' =>'09:00'],
    ['time' =>'09:30'],
];

$tasks = [
    ['time' => '07:30', 'name' => 'john'],
    ['time' => '08:00', 'name' => 'fred'],
];

The desired result should look like this:所需的结果应如下所示:

$full = [
    ['time' => '07:00', 'name' => null],
    ['time' => '07:30', 'name' => 'john'],
    ['time' => '08:00', 'name' => 'fred'],
    ['time' => '08:30', 'name' => null],
    ['time' => '09:00', 'name' => null],
    ['time' => '09:30', 'name' => null],
];

I'm looping, but I'm making a mistake somewhere.我在循环,但我在某处犯了错误。



$full = []; // this is the merge array
foreach ($wt as $k => $item) {
    $full[$k]['time'] = $item['time'];
    foreach ($tasks as $task) {
        if($item['time'] == $task['time']) {
            $full[$k]['name'] = $task['name'];  
        } else {
            $full[$k]['name'] = null;
        }
    }   
}

Here is my wrong result...这是我的错误结果...

Array
(
    [0] => Array
        (
            [time] => 07:00
            [name] => 
        )

    [1] => Array
        (
            [time] => 07:30
            [name] => 
        )

    [2] => Array
        (
            [time] => 08:00
            [name] => fred
        )

    [3] => Array
        (
            [time] => 08:30
            [name] => 
        )

    [4] => Array
        (
            [time] => 09:00
            [name] => 
        )

    [5] => Array
        (
            [time] => 09:30
            [name] => 
        )

)

I'm losing 'john'我正在失去“约翰”

It should be done so that the first array is detected with the second.应该这样做,以便用第二个阵列检测第一个阵列。 So, by matching time from the first one, fill the array 'full' with the name.因此,通过从第一个匹配时间,用名称填充数组“完整”。 Otherwise, it should be left blank.否则,应留空。

Can anyone help?任何人都可以帮忙吗? Thank you.谢谢你。

What happens here is caused because you contniue looping even when you match the result at first, and then in the other iterations you just nulling the already matched result:这里发生的事情是因为即使您首先匹配结果也继续循环,然后在其他迭代中您只是将已经匹配的结果归零:

foreach ($tasks as $task) {
    if($item['time'] == $task['time']) {
        $full[$k]['name'] = $task['name'];  
    } else {
        $full[$k]['name'] = null;
    }
} 

If you will debug it:如果要调试它:

  1. Match 07:30 to 07:30 - that's okay and you are setting the name in $k position.匹配 07:30 到 07:30 - 没关系,您在 $k position 中设置名称。
  2. Not matching 07:30 to 08:00 - insert null to name at $k position.不匹配 07:30 到 08:00 - 在 $k position 处插入 null 到名称。

if you would have a third item in your second array you were loose the second item either, so just add break after you match one value.如果您的第二个数组中有第三个项目,那么您也丢失了第二个项目,因此只需在匹配一个值后添加 break 即可。

foreach ($tasks as $task) {
    if($item['time'] == $task['time']) {
        $full[$k]['name'] = $task['name'];
        break;  
    } 
    $full[$k]['name'] = null;
    
} 

That's said I want to suggest more elegant way to do this:也就是说,我想建议更优雅的方式来做到这一点:

$wt = [
    ['time' =>'07:00'],
    ['time' =>'07:30'],
    ['time' =>'08:00'],
    ['time' =>'08:30'],
    ['time' =>'09:00'],
    ['time' =>'09:30'],
];

$tasks = [
    ['time' => '07:30', 'name' => 'john'],
    ['time' => '08:00', 'name' => 'fred'],
];

$tasks_time_values = array_column($tasks, 'time');
$tasks_name_values = array_column($tasks, 'name');

$new_tasks = array_combine($tasks_time_values, $tasks_name_values);

print_r($new_tasks);

$full = array_map(function ($w) use ($new_tasks) {
    $w['name'] = $new_tasks[$w['time']] ?? null;
    return $w;
}, $wt);

print_r($full);

Outputs:输出:

Array
(
    [07:30] => john
    [08:00] => fred
)
Array
(
    [0] => Array
        (
            [time] => 07:00
            [name] => 
        )

    [1] => Array
        (
            [time] => 07:30
            [name] => john
        )

    [2] => Array
        (
            [time] => 08:00
            [name] => fred
        )

    [3] => Array
        (
            [time] => 08:30
            [name] => 
        )

    [4] => Array
        (
            [time] => 09:00
            [name] => 
        )

    [5] => Array
        (
            [time] => 09:30
            [name] => 
        )

)

Demo Here在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM