php - WordPress PHP htmlspecialchars(get_field... 无法读取数组?

标签 php arrays wordpress advanced-custom-fields twenty-ten-theme

我正在开发一个具有两个主要功能的 WordPress 网站/博客。

  1. 创建报告。
  2. 编写最终报告。

人们可以编写报告,选择他们需要的字段并发布。然后在一天结束时,他们可以从所有报告中“编译”一份最终报告(它连接所有报告的字段)。

主题是二十个(如果它可能有用的话)。

在我的 function.php 文件中,我使用 foreach 和类似的行连接最终报告的所有内容:

$Urgences_Environnementales .= htmlspecialchars("<br/>".get_field('Urgences_Environnementales', $idnumber->ID));
$avezvous_regardé_des_indices_de_temps_violent_aujourdhui .= htmlspecialchars(get_field('avezvous_regardé_des_indices_de_temps_violent_aujourdhui', $idnumber->ID));
$quelle_est_cette_raison .= htmlspecialchars(get_field('quelle_est_cette_raison', $idnumber->ID));

每个字段一行,都是一样的。循环完成后,我更新字段:

update_field('Urgences_Environnementales',preg_replace('/(<br[\s]?[\/]?>[\s]*){2,}/', '<br/><br/>', htmlspecialchars_decode($Urgences_Environnementales)), $identificationRapport);
update_field('avezvous_regardé_des_indices_de_temps_violent_aujourdhui',preg_replace('/(<br[\s]?[\/]?>[\s]*){2,}/', '<br/><br/>', htmlspecialchars_decode($avezvous_regardé_des_indices_de_temps_violent_aujourdhui)), $identificationRapport);
update_field('quelle_est_cette_raison',preg_replace('/(<br[\s]?[\/]?>[\s]*){2,}/', '<br/><br/>', htmlspecialchars_decode($quelle_est_cette_raison)), $identificationRapport);

然后像这样打印最终报告(这是一个字段):

if(strip_tags(html_entity_decode(get_field('Urgences_Environnementales')))!=''){
   simplebox(strip_tags(html_entity_decode(get_field('Urgences_Environnementales')))!='', get_field('Urgences_Environnementales'));
}

对于那些领域,它工作得很好。

我的问题是我的所有由数组组成的字段(人们可以使用 ACF 插件选择多个选项的复选框)在我的数据库中都是空的...它们在单个报告中完美显示,但是它们在最终报告中显示为空白。

例如,这是我在数据库中看到的一个数组的单个报告:

a:4:{i:0;s:49:"L’indice d’intensité d’orage violent (STI)";i:1;s:35:"L’indice d’orage violent (TMPV)";i:2;s:34:"Potential Severe Storm Environment";i:3;s:6:"Autres";}

我的最终报告中相应的字段是空的。

有人知道如何读取这些数组并将它们正确记录在我的数据库中吗?我可以在 foreach 循环中将它们转换为字符串吗?我应该做些不同的事情吗?

如果您需要更多代码,请随时询问。我没有将所有 3 个函数(functions.phpreport.phpfinalreport.php)放入我的 WordPress 主题中因为它需要很多行,我很确定最重要的就在这里。如果我错了,我可以发布函数。

我找了又找,但我自己似乎找不到答案,所以我在这里寻求帮助。

PS:这是我的第一篇文章,如果大家有什么建议可以发给我,我会改的。

非常感谢您的帮助!

PPS:我很抱歉我的英语不好,我是法国人,来自加拿大魁北克省蒙特利尔。

最佳答案

高级自定义字段将一些值存储为序列化数组(复选框、重复器等)。您的代码假设您将得到一个字符串。正如您在回答中所建议的那样,在当前代码中解决这个问题的最简单方法是使用 is_array() 方法检查返回值的类型,然后使用另一个内部循环来处理摘要。此代码假设您只想连接所有值,您可以轻松地使用另一个数组来确保它们是唯一的,等等。

// get the value from acf
$value = get_field( 'Urgences_Environnementales', $idnumber->ID );
// if it's already an array, use that, if not make it into an array with a single element
$value_arr = ( is_array( $value ) )? $value : array( $value );
$text = ""; // reset since this is in a loop
// concatenate each checkbox value
foreach ( $value_arr as $val ){
    $text .= $val . ', ';
}
// append it to the main summary
$Urgences_Environnementales .= htmlspecialchars( "<br/>". $text );

关于php - WordPress PHP htmlspecialchars(get_field... 无法读取数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748595/

相关文章:

php - 处理查询结果中的引号 PHP

arrays - 平均阵列Powershell的一部分

wordpress - 将 robots.txt 保持为空白

php - 使用 PHP 显示 Mysql 数据给我一个空白页面

php - JQuery 不会检查类型文件的输入值是否为空?

结构数组上的 C++ qsort 内部函数

php - Woocommerce 中的更改订单备注结帐字段占位符

php - 在 Wordpress 中获取 Wordpress 子主题路径

php - Doctrine:如何根据与 Criteria 的关联按属性排序

javascript - 如何在 Javascript 中对对象数组进行排序?