问题描述
所以我认为有些东西被覆盖,但是我不确定如何停止这个并且检索循环之外的所有值。有什么想法吗?$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ gallterm,'type',array(fields=>slugs));
$ checkmissing = $ postterms [0];
print_r($ checkmissing); //检查丢失图像的条款 - 在这里工作。
}
print_r($ checkmissing); //检查丢失图像的条款 - 不在这里工作
// - 似乎只能得到最后的或第一个val。
首先初始化您想要稍后使用的变量on:
$ checkmissing = array();
然后在 foreach
里追加第一个条目对于这个数组:
foreach($ gallids as $ gallterm)
{
list ($ checkmissing [])= wp_get_post_terms($ gallterm,'type',array(fields=>slugs));
请参阅 $ checkmissing [] $ c $这是有效的防止你覆盖它。它将追加到数组中。
最后你可以在循环之后输出结果:
print_r($ checkmissing);
注意:如果 wp_get_post_terms
返回一个空数组:
foreach($ gallids as $ gallterm)
{
$ terms = wp_get_post_terms($ gallterm,'type',array(fields=>slugs))
AND list($ checkmissing [])= $ terms
;
}
So I assume something is being overwritten but I am unsure as to how to stop this and retrieve all values outside loop. Any ideas?
foreach($gallids as $gallterm)
{
$postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
$checkmissing = $postterms[0];
print_r($checkmissing); // Check Terms for missing images - works here.
}
print_r($checkmissing); // Check Terms for missing images - not working here
// - seems to be getting last or first val only.
First of all initialize the variable you want to use later on:
$checkmissing = array();
Then inside the foreach
append the first entry of the post terms to that array:
foreach($gallids as $gallterm)
{
list($checkmissing[]) = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
}
See $checkmissing[]
, that is what effectively will prevent that you overwrite it. It will append each to the array.
Finally you can output the result after the loop:
print_r($checkmissing);
Note: You should do some additional handling if wp_get_post_terms
returns an empty array:
foreach($gallids as $gallterm)
{
$terms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"))
AND list($checkmissing[]) = $terms
;
}
这篇关于返回存储在foreach循环外的var中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!