将2个数组与array_diff进行比较

时间:2014-11-09 14:48:12

标签: php arrays

我需要比较两个数组:

第一个数组包含zipcodes keysdates values

Array ( [1000] => Array ( [0] => 2014-12-01 am [1] => 2014-12-01 pm [2] => 2014-12-05 am [3] => 2014-12-05 pm [4] => 2014-12-08 am [5] => 2014-12-08 pm [6] => 2014-12-12 am [7] => 2014-12-12 pm [8] => 2014-12-15 am [9] => 2014-12-15 pm [10] => 2014-12-19 am [11] => 2014-12-19 pm [12] => 2014-12-22 am [13] => 2014-12-22 pm ) [1020] => Array ( [0] => 2014-12-01 am [1] => 2014-12-01 pm [2] => 2014-12-02 am [3] => 2014-12-02 pm [4] => 2014-12-05 am [5] => 2014-12-05 pm [6] => 2014-12-08 am [7] => 2014-12-08 pm [8] => 2014-12-09 am [9] => 2014-12-09 pm [10] => 2014-12-12 am [11] => 2014-12-12 pm [12] => 2014-12-15 am [13] => 2014-12-15 pm [14] => 2014-12-16 am [15] => 2014-12-16 pm [16] => 2014-12-19 am [17] => 2014-12-22 am [18] => 2014-12-22 pm [19] => 2014-12-23 am [20] => 2014-12-23 pm ) (...) ) 

然后第二个数组的日期在数据库中至少存储了4次,如下例所示:

Array ( [0] => 2014-12-01 pm [1] => 2014-12-12 am )

我正在尝试使用array_diff创建一个没有此日期的新数组,但它不起作用。

我错过了什么?任何想法如何使这项工作?

提前致谢

编辑:

这里是完整的代码

global $woocommerce;

$dates = array(
    '1000' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-05 am', '2014-12-05 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-19 am', '2014-12-19 pm', '2014-12-22 am','2014-12-22 pm'),
    '1020' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-02 am', '2014-12-02 pm', '2014-12-05 am', '2014-12-05 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-19 am', '2014-12-22 am', '2014-12-22 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1030' => array('2014-12-05 am', '2014-12-05 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-19 am', '2014-12-19 pm'),
    '1030' => array('2014-12-05 am', '2014-12-05 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-19 am', '2014-12-19 pm'),
    '1040' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1050' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1060' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1070' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1080' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1081' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1082' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1083' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1090' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1120' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-05 am', '2014-12-05 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-19 am', '2014-12-19 pm', '2014-12-22 am', '2014-12-22 pm'),
    '1130' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-22 am', '2014-12-22 pm'),
    '1140' => array('2014-12-05 am', '2014-12-05 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-19', 'am'),
    '1150' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1160' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1170' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1180' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1190' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1200' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1210' => array('2014-12-05 am', '2014-12-05 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-19 am', '2014-12-19 pm'),
    '1620' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1630' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1650' => array('2014-12-03 am', '2014-12-03 pm', '2014-12-10 am', '2014-12-10 pm', '2014-12-17 am', '2014-12-17 pm'),
    '1702' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1780' => array('2014-12-02 am', '2014-12-02 pm', '2014-12-09 am', '2014-12-09 pm', '2014-12-16 am', '2014-12-16 pm', '2014-12-23 am', '2014-12-23 pm'),
    '1800' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-22 am', '2014-12-22 pm'),
    '1850' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-22 am', '2014-12-22 pm'),
    '1853' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-22 am', '2014-12-22 pm'),
    '1930' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1932' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1950' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm'),
    '1970' => array('2014-12-04 am', '2014-12-04 pm', '2014-12-11 am', '2014-12-11 pm', '2014-12-18 am', '2014-12-18 pm')
);  

$args = array(
    'post_type' => 'shop_order',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'shop_order_status',
            'field' => 'slug',
            'terms' => array('pending', 'processing', 'completed', 'on-hold')
        )
    )
);

$loop = new WP_Query($args);

$delivery_dates = array();

while ($loop->have_posts()) : $loop->the_post();
    $order_id = $loop->post->ID;

    $order = new WC_Order($order_id);

    if(array_key_exists($order->billing_delivery_date, $delivery_dates))
        $delivery_dates[$order->billing_delivery_date]++;
    else
        $delivery_dates[$order->billing_delivery_date] = 1;

endwhile;

$max_deliveries = 4;
$result = array_filter(
    $delivery_dates, function ($value) use($max_deliveries) {
        return ($value >= $max_deliveries);
    }
);

$reverse = array_flip($result);

$delivery_dates_db = array_values($reverse);

$new_dates = array_diff($dates, $delivery_dates_db);

print_r($new_dates);

1 个答案:

答案 0 :(得分:0)

所以,目前你有一个不必要的数组嵌套。

看看第一个条目:

<?php
$dates = array(
'1000' => array(
    array('2014-12-01 am', '2014-12-01 pm', '2014-12-05 am', '2014-12-05 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-19 am', '2014-12-19 pm', '2014-12-22 am','2014-12-22 pm')
)
....

你可以这样:

<?php
$dates = array(
'1000' => array('2014-12-01 am', '2014-12-01 pm', '2014-12-05 am', '2014-12-05 pm', '2014-12-08 am', '2014-12-08 pm', '2014-12-12 am', '2014-12-12 pm', '2014-12-15 am', '2014-12-15 pm', '2014-12-19 am', '2014-12-19 pm', '2014-12-22 am','2014-12-22 pm'),
....

如果您打算使用array_diff,那么您必须迭代$ date来比较其中的每个值。 (此代码仅在没有嵌套数组的情况下有效。)

<?php
foreach($dates as $key => value){
  $dates[$key] = array_diff($value, $delivery_dates_db);
}

此代码应与嵌套数组一起使用。

<?php
foreach($dates as $key => value){
  $dates[$key] = array_diff($value[0], $delivery_dates_db);
}

我不确定你为什么要嵌套另一个数组。