【问题标题】:A variable array into separate items一个变量数组分成单独的项目
【发布时间】: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


【解决方案1】:

发现我做错了什么......

刚刚将 json_decode 添加到初始数组中...

$current_endMaps = json_decode(get_post_meta( $post->ID, "_tsb_postmeta_end_maps", true ));
$arrlength = count($current_endMaps);
for($x = 0; $x < $arrlength; $x++) {
    echo "<img src='".$current_endMaps[$x]."' width='100%'>";
}

然后将回显输出作为图像完成。现在可以使用了。

【讨论】:

    【解决方案2】:

    我相信您需要提供 false 作为 get_post_meta() 的第三个参数来获取数组

    https://developer.wordpress.org/reference/functions/get_post_meta/

    $single (bool) (可选)是否返回单个值。默认值:假

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 2012-02-04
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-16
      相关资源
      最近更新 更多