返回存储在foreach循环外的var中的所有值 [英] Return all values stored in var outside foreach loop

查看:136
本文介绍了返回存储在foreach循环外的var中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我认为有些东西被覆盖,但是我不确定如何停止这个并且检索循环之外的所有值。有什么想法吗?

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 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 [] 追加到数组中。



最后你可以在循环之后输出结果:

  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中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆