对于每个循环不能按预期使用对象

时间:2014-03-19 13:47:37

标签: php if-statement foreach

我正在循环一个对象,当一个对象键等于一个字符串时,它回应了一些东西。但是,它在if statement时无效。

$get_categories = get_categories(); // gets an object (with the key)

foreach ($get_categories as $key) {
    $each_cat = $key->name;
    echo $each_cat; // echoes 'beauty', 'celebrity', 'lifestyle'
    if ($each_cat == 'fashion') { // this is FALSE, even though it prints 'fashion' above
        echo 'Yes, this is the fashion string'; // this does not get printed
    }
}

这是它循环的对象:

ARRAY (SIZE=7)
  0 => 
    OBJECT(STDCLASS)[311]
      PUBLIC 'TERM_ID' => &STRING '9' (LENGTH=1)
      PUBLIC 'NAME' => &STRING 'BEAUTY' (LENGTH=6)
      PUBLIC 'SLUG' => &STRING 'BEAUTY' (LENGTH=6)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '9' (LENGTH=1)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '0' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '1' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '9' (LENGTH=1)
      PUBLIC 'CATEGORY_COUNT' => &STRING '1' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'BEAUTY' (LENGTH=6)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'BEAUTY' (LENGTH=6)
      PUBLIC 'CATEGORY_PARENT' => &STRING '0' (LENGTH=1)
  1 => 
    OBJECT(STDCLASS)[310]
      PUBLIC 'TERM_ID' => &STRING '11' (LENGTH=2)
      PUBLIC 'NAME' => &STRING 'CELEBRITY' (LENGTH=9)
      PUBLIC 'SLUG' => &STRING 'CELEBRITY' (LENGTH=9)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '11' (LENGTH=2)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '0' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '3' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '11' (LENGTH=2)
      PUBLIC 'CATEGORY_COUNT' => &STRING '3' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'CELEBRITY' (LENGTH=9)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'CELEBRITY' (LENGTH=9)
      PUBLIC 'CATEGORY_PARENT' => &STRING '0' (LENGTH=1)
  3 => 
    OBJECT(STDCLASS)[308]
      PUBLIC 'TERM_ID' => &STRING '4' (LENGTH=1)
      PUBLIC 'NAME' => &STRING 'FASHION' (LENGTH=7)
      PUBLIC 'SLUG' => &STRING 'FASHION' (LENGTH=7)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '4' (LENGTH=1)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '0' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '4' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '4' (LENGTH=1)
      PUBLIC 'CATEGORY_COUNT' => &STRING '4' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'FASHION' (LENGTH=7)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'FASHION' (LENGTH=7)
      PUBLIC 'CATEGORY_PARENT' => &STRING '0' (LENGTH=1)
  5 => 
    OBJECT(STDCLASS)[306]
      PUBLIC 'TERM_ID' => &STRING '10' (LENGTH=2)
      PUBLIC 'NAME' => &STRING 'LIFESTYLE' (LENGTH=9)
      PUBLIC 'SLUG' => &STRING 'LIFESTYLE' (LENGTH=9)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '10' (LENGTH=2)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '0' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '3' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '10' (LENGTH=2)
      PUBLIC 'CATEGORY_COUNT' => &STRING '3' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'LIFESTYLE' (LENGTH=9)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'LIFESTYLE' (LENGTH=9)
      PUBLIC 'CATEGORY_PARENT' => &STRING '0' (LENGTH=1)
  6 => 
    OBJECT(STDCLASS)[305]
      PUBLIC 'TERM_ID' => &STRING '1' (LENGTH=1)
      PUBLIC 'NAME' => &STRING 'NONE' (LENGTH=4)
      PUBLIC 'SLUG' => &STRING 'NONE' (LENGTH=4)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '1' (LENGTH=1)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '0' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '2' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '1' (LENGTH=1)
      PUBLIC 'CATEGORY_COUNT' => &STRING '2' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'NONE' (LENGTH=4)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'NONE' (LENGTH=4)
      PUBLIC 'CATEGORY_PARENT' => &STRING '0' (LENGTH=1)
  8 => 
    OBJECT(STDCLASS)[303]
      PUBLIC 'TERM_ID' => &STRING '16' (LENGTH=2)
      PUBLIC 'NAME' => &STRING 'TOP' (LENGTH=3)
      PUBLIC 'SLUG' => &STRING 'TOP' (LENGTH=3)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '16' (LENGTH=2)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '0' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '2' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '16' (LENGTH=2)
      PUBLIC 'CATEGORY_COUNT' => &STRING '2' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'TOP' (LENGTH=3)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'TOP' (LENGTH=3)
      PUBLIC 'CATEGORY_PARENT' => &STRING '0' (LENGTH=1)
  9 => 
    OBJECT(STDCLASS)[302]
      PUBLIC 'TERM_ID' => &STRING '8' (LENGTH=1)
      PUBLIC 'NAME' => &STRING 'TRENDS' (LENGTH=6)
      PUBLIC 'SLUG' => &STRING 'TRENDS' (LENGTH=6)
      PUBLIC 'TERM_GROUP' => STRING '0' (LENGTH=1)
      PUBLIC 'TERM_TAXONOMY_ID' => STRING '8' (LENGTH=1)
      PUBLIC 'TAXONOMY' => STRING 'CATEGORY' (LENGTH=8)
      PUBLIC 'DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'PARENT' => &STRING '4' (LENGTH=1)
      PUBLIC 'COUNT' => &STRING '1' (LENGTH=1)
      PUBLIC 'CAT_ID' => &STRING '8' (LENGTH=1)
      PUBLIC 'CATEGORY_COUNT' => &STRING '1' (LENGTH=1)
      PUBLIC 'CATEGORY_DESCRIPTION' => &STRING '' (LENGTH=0)
      PUBLIC 'CAT_NAME' => &STRING 'TRENDS' (LENGTH=6)
      PUBLIC 'CATEGORY_NICENAME' => &STRING 'TRENDS' (LENGTH=6)
      PUBLIC 'CATEGORY_PARENT' => &STRING '4' (LENGTH=1)

1 个答案:

答案 0 :(得分:0)

尝试

foreach ($get_categories as $key=>$val) {
    $each_cat = $val->name;
    echo $each_cat; // echoes 'beauty', 'celebrity', 'lifestyle'
    if ($each_cat == 'fashion') { // this is FALSE, even though it prints 'fashion' above
        echo 'Yes, this is the fashion string'; // this does not get printed
    }
}

此处$key是数组索引0,1,2... $val是作为对象的那些键的对应值。

这样做$key->name不会返回任何内容。

if ($each_cat == 'fashion') 

代码块是正确的,因为你的echo显示它们都是小写的,如果你正在查看它们都在CAPS中的变量转储,包括PUBLIC,Key name等。它们不应该大写。