我不确定我是否提出了正确的问题,但我一直难以理解如何防止数值在数组中不存在时重复出现。以下是该数组的示例:
Array (
[meta_id] => 995
[order_item_id] => 83
[meta_key] => Booking Date
[meta_value] => February 7, 2015
)
Array (
[meta_id] => 996
[order_item_id] => 83
[meta_key] => Booking Time
[meta_value] => 7:00 am
)
Array (
[meta_id] => 997
[order_item_id] => 83
[meta_key] => First Name - First Name
[meta_value] => Dan
)
Array (
[meta_id] => 998
[order_item_id] => 83
[meta_key] => Church Information - Church Name
[meta_value] => Colonial Hills Baptist Church
)
Array (
[meta_id] => 1010
[order_item_id] => 84
[meta_key] => Booking Date
[meta_value] => January 8, 2015
)
Array (
[meta_id] => 1011
[order_item_id] => 84
[meta_key] => Booking Time
[meta_value] => 2:00 am
)
Array (
[meta_id] => 1012
[order_item_id] => 84
[meta_key] => First Name - First Name
[meta_value] => Gary
)
Array (
[meta_id] => 1013
[order_item_id] => 84
[meta_key] => Church Information - Church Name
[meta_value] => Victory Bible Church
)
Array (
[meta_id] => 973
[order_item_id] => 81
[meta_key] => Booking Date
[meta_value] => March 7, 2015
)
Array (
[meta_id] => 974
[order_item_id] => 81
[meta_key] => Booking Time
[meta_value] => 3:00 am
)
Array (
[meta_id] => 975
[order_item_id] => 81
[meta_key] => First Name - First Name
[meta_value] => Dan
)
Array (
[meta_id] => 925
[order_item_id] => 77
[meta_key] => Booking Date
[meta_value] => January 1, 2015
)
Array (
[meta_id] => 926
[order_item_id] => 77
[meta_key] => Booking Time
[meta_value] => 10:00 am
)
Array (
[meta_id] => 927
[order_item_id] => 77
[meta_key] => First Name - First Name
[meta_value] => Kent
)
...
您可以在这些数组中看到它们按order_item_id键分组。并且您还将看到order_item_id为81和77的数组没有教会信息 - 教会名称meta_key和meta_value。
以下是生成这些数组的代码:
foreach( $order_items as $order_item ) {
$item_meta = $wpdb->get_results("
SELECT *
FROM {$wpdb->prefix}woocommerce_order_itemmeta
WHERE order_item_id = " . $order_item->order_item_id . "
");
$meta_array = objectToArray( $item_meta );
foreach( $meta_array as $data ) {
if( $data['meta_key'] == 'Booking Date' )
$date = $data['meta_value'];
if( $data['meta_key'] == 'Booking Time' )
$time = $data['meta_value'];
if( $data['meta_key'] == 'First Name - First Name' )
$fname = $data['meta_value'];
if( $data['meta_key'] == 'Church Information - Church Name' )
$church = $data['meta_value'];
if( $data['meta_key'] == 'Church Information - City' )
$city = $data['meta_value'];
if( $data['meta_key'] == 'Church Information - State' )
$state = $data['meta_value'];
}
echo $fname . ', ' . $church . '<br>';
}
结果如下:
Dan, Colonial Hills Baptist Church
Gary, Victory Bible Church
Dan, Victory Bible Church
Kent, Victory Bible Church
正如您所看到的,$ church变量的结果正在重复并显示在该值不存在的区域中。在上面的结果中,只有加里应该具有与之相关的“胜利圣经教会”的价值。
有人可以帮我解决这个问题吗?
由于
答案 0 :(得分:0)
您需要在每个周期初始化变量,如下所示:
foreach( $order_items as $order_item ) {
$item_meta = $wpdb->get_results("
SELECT *
FROM {$wpdb->prefix}woocommerce_order_itemmeta
WHERE order_item_id = " . $order_item->order_item_id . "
");
$meta_array = objectToArray( $item_meta );
$date = "";
$time = "";
fname = "";
$church = "";
$city = "";
$state = "";
foreach( $meta_array as $data ) {
if( $data['meta_key'] == 'Booking Date' )
$date = $data['meta_value'];
else if( $data['meta_key'] == 'Booking Time' )
$time = $data['meta_value'];
else if( $data['meta_key'] == 'First Name - First Name' )
$fname = $data['meta_value'];
else if( $data['meta_key'] == 'Church Information - Church Name' )
$church = $data['meta_value'];
else if( $data['meta_key'] == 'Church Information - City' )
$city = $data['meta_value'];
else if( $data['meta_key'] == 'Church Information - State' )
$state = $data['meta_value'];
}
echo $fname . ', ' . $church . '<br>';
}