PHP:从多数组获取值

时间:2015-04-02 19:30:47

标签: php arrays

我正在尝试从Array中检索值。这是代码:

我的数组如下:

$video_categories = get_the_terms( get_the_ID(), 'videos_cat', ', ' );

打印结果时:

Array
    (
        [22] => stdClass Object
            (
                [term_id] => 22
                [name] => فيديو
                [slug] => videos
                [term_group] => 0
                [term_taxonomy_id] => 47
                [taxonomy] => videos_cat
                [description] => 
                [parent] => 0
                [count] => 383
                [object_id] => 26138
                [filter] => raw
            ),
        [23] => stdClass Object
            (
                [term_id] => 23
                [name] => العاب
                [slug] => videos
                [term_group] => 0
                [term_taxonomy_id] => 47
                [taxonomy] => videos_cat
                [description] => 
                [parent] => 0
                [count] => 383
                [object_id] => 26138
                [filter] => raw
            )

    )

如何在变量中获取term_id的值来打印它?

1 个答案:

答案 0 :(得分:1)

以正常array[index]方式访问数组的索引。这包含一个对象,因此您需要使用对象运算符来访问它的属性和方法,如object->propertyobject->method()

从您的行为看,它就像是:

echo $categories[22]->term_id;