混合数组和对象

时间:2012-05-23 18:17:49

标签: php

我有一个这样的数组:

Array
(
    [count] => 2
    [0] => stdClass Object
        (
            [term_id] => 3109
            [name] => Aliens: Colonial Marines
            [slug] => aliens-colonial-marines
        )

    [1] => stdClass Object
        (
            [term_id] => 228
            [name] => Dragon Quest X: Rise of the Five Tribes
            [slug] => dragon-quest-x-rise-of-the-five-tribes
        )

)

我需要运行一个循环来抓住" slug"每个元素的价值。 我怎么能完成这个结果?

1 个答案:

答案 0 :(得分:3)

foreach ($array as $key => $value) {
    if (is_object ($value)) echo $value->slug;
}

它只是遍历数组$array,如果元素是一个对象(因此它不会尝试解析count),它会回显slug