php WordPress 回显数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5358001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:08:27  来源:igfitidea点击:

WordPress echo out array

phpwordpress

提问by Cameron

I have the following line of code: $terms = get_the_terms( $the_post->ID, 'posts_tags' ); echo $terms;The idea is to echo out the array in the format of the tags but instead it just returns the word Array?

我有以下代码行:$terms = get_the_terms( $the_post->ID, 'posts_tags' ); echo $terms;这个想法是以标签的格式回显数组,但它只是返回单词 Array?

How do I do this? The tags should be presented like this: 'tag1, tag2, tag3'

我该怎么做呢?标签应如下所示:'tag1, tag2, tag3'

回答by Nanne

try

尝试

foreach($terms as $term){
    echo $term;
}

you might want to add something to separate them, like a $echo ","or something, but you get the idea
You can also use this

你可能想添加一些东西来分隔它们,比如 a$echo ","或什么,但你明白了
你也可以使用这个

$termsString = implode (',' , $terms);
echo $termsString;

As requested:

按照要求:

The terms is indeed an array, see for instance @ProdigySim 's answer for how it looks. You could indeed print them for debuggin purposes with var_dumpor print_r, but that would not help you in a production environment.

这些术语确实是一个数组,例如,请参阅@ProdigySim 的答案,了解它的外观。您确实可以使用var_dump或出于调试目的打印它们print_r,但这在生产环境中对您没有帮助。

Assuming that it is not an associative array, you could find the first tag like this:

假设它不是关联数组,您可以找到第一个标签,如下所示:

echo $terms[0]

and the rest like

其余的喜欢

echo $terms[1]

Right up until the last one (count($terms)-1).
The foreach prints each of these in order. The second, as you can find in the manualjust makes one string of them.

直到最后一个(count($terms)-1)。
foreach 按顺序打印每一个。第二个,正如您在手册中所见,只制作其中的一串。

In the end, as you asked in the comments: Just paste this.

最后,正如您在评论中所问:只需粘贴即可。

$terms = get_the_terms( $the_post->ID, 'posts_tags' ); 
$termsString = implode (',' , $terms);
echo $termsString;

回答by ProdigySim

You're looking for print_r()

您正在寻找print_r()

$a = array ('a' => 'apple', 'b' => 'banana');
print_r ($a);

outputs

产出

Array
(
    [a] => apple
    [b] => banana
)

回答by Ryre

Arrays are an amazing feature in every programming language I've ever used. I highly suggest spending some time reading up on them, especially if you're customizing wordpress.

在我使用过的每一种编程语言中,数组都是一个了不起的特性。我强烈建议花一些时间阅读它们,特别是如果您正在自定义 wordpress。

The reason it only echos array is because echo can only return strings. Try print_r($terms) or var_dump($terms) for more information.

它只回显数组的原因是因为回显只能返回字符串。尝试 print_r($terms) 或 var_dump($terms) 以获取更多信息。

http://php.net/manual/en/language.types.array.php

http://php.net/manual/en/language.types.array.php