doutui9606 2017-01-16 01: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 02: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 有人知道这是什么问题吗
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部