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

保存每个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条回答 默认 最新

相关推荐 更多相似问题