doutui9606 2017-01-16 09:46
浏览 131
已采纳

PHP:在循环中获取第一个数组

I have the following code:

$customer_orders = get_posts(array(
    'numberposts' => -1,
    'meta_key' => '_customer_user',
    'meta_value' => get_current_user_id(),
    'post_type' => 'shop_order',
    'post_status' => array_keys(wc_get_order_statuses()),
    )
);

$last_post_date;
$loop = new WP_Query($customer_orders);
foreach ($customer_orders as $orderItem)
{
    $order = wc_get_order($orderItem->ID);
    $last_post_date = $orderItem->post_date;
}

echo $last_post_date;

I want to print the $last_post_date, which is created from an order made by a customer.

So if a customer makes 2 order I will get an array [0] and [1].

But at the moment $last_post_date is not printing the post_date from array[0].

It is always printing the post date from the order that was made first, not last,

Thanks for the help!

  • 写回答

2条回答 默认 最新

  • douhuan5073 2017-01-16 10:27
    关注

    As you have 2 order so the $last_post_date is getting replaced by substituent post_date. So if you want to extract only the first order date then you can add a checking.

    Try this code:

    foreach ($customer_orders as $key => $orderItem) //<-- added $key
    {
        if ($key == 0) //only for first element.
        {
            $last_post_date = $orderItem->post_date;
        }
        $order = wc_get_order($orderItem->ID);
    }
    
    echo $last_post_date;
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别