PHP:按项目meta(数组中的数组)对数组(woocommerce项目)进行排序

时间:2016-03-15 14:01:39

标签: php woocommerce

我试图通过每个项目中包含的元数对每个订单项的第二个foreach循环返回的项目数组进行排序(每个$ custom_item都是它自己的数组)。我目前正在使用以下代码:

// Order variables
$user_id_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    'numberposts' => $order_count,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types( 'view-orders' ),
    'post_status' => array_keys( wc_get_order_statuses() )
) ) );

if ( $user_id_orders ) {
    foreach ( $user_id_orders as $user_id_order ) {
        // Foreach order variables
        $order = wc_get_order( $user_id_order );
        $order->populate( $user_id_order );
        $item_count = $order->get_item_count();
        $order_ID = $order->get_order_number();

        $custom_order = wc_get_order( $order_ID );
        $co = $custom_order->get_items();
        $co = array();

        foreach( $custom_order->get_items() as $custom_item_id => $custom_item ) {
                            $co[$custom_item_id] = $custom_item['item_meta']['Date'];
                            arsort($co);
                            print_r($co);
                            echo '<br>';
        }

        array_multisort($co, SORT_ASC, $custom_order->get_items());
    }
}

此代码返回以下结果:

Array ( [57] => Array ( [0] => 20/06/2016 ) ) 
Array ( [56] => Array ( [0] => 21/06/2016 ) ) 
Array ( [55] => Array ( [0] => 21/06/2016 ) ) 
Array ( [54] => Array ( [0] => 03/04/2011 ) ) 
Array ( [53] => Array ( [0] => 22/06/2016 ) ) 
Array ( [52] => Array ( [0] => 22/06/2016 ) ) 
Array ( [51] => Array ( [0] => 22/06/2016 ) ) 
Array ( [50] => Array ( [0] => 20/06/2016 ) ) 
Array ( [49] => Array ( [0] => 23/06/2016 ) ) 
Array ( [48] => Array ( [0] => 23/06/2016 ) ) 
Array ( [46] => Array ( [0] => 23/06/2016 ) ) 
Array ( [45] => Array ( [0] => 22/06/2016 ) ) 
Array ( [43] => Array ( [0] => 20/06/2016 ) ) 
Array ( [42] => Array ( [0] => 20/06/2016 ) ) 
Array ( [41] => Array ( [0] => 21/06/2016 ) ) 
Array ( [40] => Array ( [0] => 21/06/2016 ) ) 

每个Array都是循环返回的自定义项,我想按检索的日期对它们进行排序。

到目前为止,循环内部或外部都没有工作,也没有array_multisort。

如果需要,我可以根据要求提供更多信息。

1 个答案:

答案 0 :(得分:3)

您可能希望使用usort,这将允许您定义自己的排序功能。

http://php.net/manual/en/function.usort.php