【发布时间】:2018-04-10 06:11:18
【问题描述】:
我不确定这段代码哪里出错了。我只是想将一个数组从一个变量输出到它的单独项目中。
例如:
$current_endMaps = get_post_meta($post->ID, "_tsb_postmeta_end_maps", true);
会给我以下输出...
["http://localhost/lmn-beta/wp-content/uploads/2017/10/image1.png","http://localhost/lmn-beta/wp-content/uploads/2017/10/image2.png"]
但我试图输出如下...
http://localhost/lmn-beta/wp-content/uploads/2017/10/image1.png
http://localhost/lmn-beta/wp-content/uploads/2017/10/image2.png
这是我目前所拥有的......但这只是输出这个
[
这是我正在使用的代码..
$current_endMaps = get_post_meta($post->ID, "_tsb_postmeta_end_maps", true);
$arrlength = count($current_endMaps);
for($x = 0; $x < $arrlength; $x++) {
echo $current_endMaps[$x];
echo "<br>";
}
任何帮助将不胜感激。
【问题讨论】:
-
它不工作吗?有一种更简单的方法可以在 PHP 中循环数组,但这应该可以工作。
-
有什么更简单的方法?我假设 wordpress 中的某些东西只是弄乱了输出。
-
[输出可疑。查看结果输出的页面源。您是否在其中看到其他内容,例如损坏的、未渲染的 HTML?而不是echo $current_endMaps[$x]使用var_dump($current_endMals[$x]进行调试。 -
此外,在 PHP 中迭代数组时,很少使用递增
for ()循环。这应该不会影响您的输出,但我建议您使用foreach($current_endMaps as $current_endMap) {...}并以echo $current_endMap的形式访问内部 -
我不知道。一定是出了点问题,因为我以前做过很多次,但输出要么相同,要么显示错误。
标签: php arrays wordpress variables foreach