php 如何比较两个数组并替换为日期?

xjreopfe  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(154)

数组:1

Array
(
    [0] => Array
        (
            [date] => 2022-12-09
            [verify] => 0
            [unverify] => 0
        )

    [1] => Array
        (
            [date] => 2022-12-08
            [verify] => 0
            [unverify] => 0
        )

    [2] => Array
        (
            [date] => 2022-12-07
            [verify] => 0
            [unverify] => 0
        )

    [3] => Array
        (
            [date] => 2022-12-06
            [verify] => 0
            [unverify] => 0
        )

    [4] => Array
        (
            [date] => 2022-12-05
            [verify] => 0
            [unverify] => 0
        )

    [5] => Array
        (
            [date] => 2022-12-04
            [verify] => 0
            [unverify] => 0
        )

    [6] => Array
        (
            [date] => 2022-12-03
            [verify] => 0
            [unverify] => 0
        )

)

阵列:2

Array
(
    [0] => Array
        (
            [date] => 2022-12-09
            [verify] => 10
            [unverify] => 6
        )

    [1] => Array
        (
            [date] => 2022-12-08
            [verify] => 2
            [unverify] => 5
        )

)

我终于想要这种数组了。
实验结果:

Array
(
    
   [0] => Array
        (
            [date] => 2022-12-09
            [verify] => 10
            [unverify] => 6
        )

    [1] => Array
        (
            [date] => 2022-12-08
            [verify] => 2
            [unverify] => 5
        )

    [2] => Array
        (
            [date] => 2022-12-07
            [verify] => 0
            [unverify] => 0
        )

    [3] => Array
        (
            [date] => 2022-12-06
            [verify] => 0
            [unverify] => 0
        )

    [4] => Array
        (
            [date] => 2022-12-05
            [verify] => 0
            [unverify] => 0
        )

    [5] => Array
        (
            [date] => 2022-12-04
            [verify] => 0
            [unverify] => 0
        )

    [6] => Array
        (
            [date] => 2022-12-03
            [verify] => 0
            [unverify] => 0
        )

)

请帮帮我。
这是两个数组,我想比较(或任何东西)两个数组,所以我得到我想要的结果。在数组2中的所有记录都被替换为数组1中的日期(索引),所以我得到结果

9fkzdhlc

9fkzdhlc1#

// The arrays to compare
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

// The callback function that will be applied to each element of the arrays
function compareAndReplaceByDate($value1, $value2) {
  // Compare the values of the two arrays
  if ($value1 > $value2) {
    // If the first value is greater, return the date
    return date('Y-m-d');
  } else {
    // Otherwise, return the second value
    return $value2;
  }
}

// Use array_map() to apply the compareAndReplaceByDate() function to each element of the arrays
$result = array_map('compareAndReplaceByDate', $array1, $array2);

// Print the resulting array
print_r($result);

相关问题