简体   繁体   English

如何在foreach之外检索stdobject属性

[英]How to retrieve stdobject property outside the foreach

Array
    (
        [0] => stdClass Object
            (
                [rating] => 2
            )

    )

I'm trying to get like this 我正在努力像这样

foreach ($star_1 as $key) 
        {
            print_r($key->rating);
        }

How do I get the rating value outside the foreach 我如何获得Foreach以外的rating

This value is an array containing elements of type stdClass . 该值是一个包含stdClass类型的元素的数组。 These elements have fields that can be accessed using element->field_name . 这些元素具有可以使用element->field_name进行访问的字段。 Since you want to access the rating of the first element in your array (= at index 0), you should use 由于您要访问数组中第一个元素的等级 (=在索引0处),因此应使用

$array[0]->rating

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM