为每个ACF转发器字段行保存值,并将所有值放入一个数组(PHP) [英] Save value for each ACF repeater field row and place all values into one array (PHP)

查看:72
本文介绍了为每个ACF转发器字段行保存值,并将所有值放入一个数组(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WordPress和高级自定义字段来构建网站。

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.

我试图将这些子字段保存到 $ dates 数组。但是,此 $ dates 数组通过 var_dump()输出只有一个值的多个数组。

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().

$ 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" }

使用以下代码,我正在尝试遍历 $ dates 数组,并将值转换为 $ month 变量,该变量输出月名称。从日期到月份名称的转换有效,但是我需要将每个重复行的这些 $ month 值放入一个 $ months 数组。

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.

我尝试在下面创建一个 $ months 数组并添加每个<$ c $该数组的c> $ month 值。此代码为每个中继器行输出单独的数组,该数组中只有一个月的值。 (与我在 $ dates 数组中遇到的问题相同。)

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;
}

?>


推荐答案

您要在每个循环中重置months数组。只需将分配移到 while 循环之外即可。

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;

这篇关于为每个ACF转发器字段行保存值,并将所有值放入一个数组(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆