dougong9987 2018-05-03 19:05
浏览 75
已采纳

保存每个ACF转发器字段行的值并将所有值放入一个数组(PHP)

I am using WordPress and Advanced Custom Fields to build a website.

I have an Advanced Custom Field repeater field for an event listing on my website. For each event row, there is a sub field to input a date.

I have attempted to save those sub fields into a $dates array. However, this $dates array is outputting several arrays with only one value with var_dump().

Output of $dates:

array(1) { [0]=> string(20) "June 5, 2018 5:00 pm" }
array(1) { [0]=> string(22) "June 15, 2018 12:00 am" }
array(1) { [0]=> string(22) "July 13, 2018 12:00 am" }
array(1) { [0]=> string(22) "July 13, 2018 12:00 am" }
array(1) { [0]=> string(22) "July 27, 2018 12:00 am" }
array(1) { [0]=> string(24) "August 18, 2018 12:00 am" }

With the code below, I am trying to iterate through a $dates array and convert the values into a $month variable that outputs a month name. The conversion from the date to month name is working, but I need to place these $month values for each repeater row into one $months array.

I tried to create a $months array below and add each $month value to that array. This code outputs separate arrays for each repeater row with only one month value in the array. (Same issue as I have with the $dates array.)

I'm not sure how to accomplish this or if I am looking at this issue the wrong way. Any help would be appreciated!

<?php if (have_rows('events')):

while (have_rows('events')) : the_row();

$dates = array();
$dates[] = get_sub_field('date_time');

foreach ($dates as $date) {
  $timestamp = strtotime($date);
  $month = date('F', $timestamp);
  /* this code below does not work as intended */
  $months = array();
  $months[] = $month;
}

?>
  • 写回答

1条回答 默认 最新

  • dongyijing2353 2018-05-03 19:14
    关注

    You're resetting the months array at every loop. Simply move the assignation outside of your while loop.

    $months = array();
    
    while (have_rows('events')) : the_row();
    
        foreach ($dates as $date) {
            $timestamp = strtotime($date);
            $month = date('F', $timestamp);
            $months[] = $month;
        }
    
    endwhile;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测