I am creating a WooCommerce plugin, which detects duplicate guest orders based on date and email address. I was able to create an array, where are all data which i need. If there is a duplicate the order id is added to the correct email address.
But now I want to delete all the arrays where are only 1 order id present.
CODE:
$i = 0;
$big_order = array();
foreach($order_ids as $id) {
$order = new WC_Order($id);
//print_r ($order);
if(array_search($order->billing_email, array_column($big_order, 'email')) !== false){
$key = array_search($order->billing_email, array_column($big_order, 'email'));
$big_order['row-'.$key]['order_id'][] = $id;
} else {
$big_order['row-'.$i]['email'] = $order->billing_email;
$big_order['row-'.$i]['order_id'][] = $id;
}
$i++;
}
print_r ($big_order);
OUTPUT:
Array
(
[row-0] => Array
(
[email] => [email protected]
[order_id] => Array
(
[0] => 14
)
)
[row-1] => Array
(
[email] => [email protected]
[order_id] => Array
(
[0] => 13
)
)
[row-2] => Array
(
[email] => [email protected]
[order_id] => Array
(
[0] => 12
[1] => 10
)
)
)
In this case I only need row-2 array. Thanks for the help.
Use array_filter()
and count()
:
$big_order = array_filter($big_order, function (array $row) {
return 1 < count($row['order_id']);
});
For reference, see:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments