在PHP对象中访问数组

dig*_*tal 0 php arrays object

我有以下PHP对象,但我很难从对象中获取数组项.

exampleBatch Object (
[file_path:protected] => 
[title:protected] => 
[description:protected] => 
[link:protected] => 
[items:protected] => Array ( ) 
[raw:protected] => data/example 
[feed_nid:protected] => 
Array ( 
    [0] => Array ( [path] => data/example/example/ [filename] => file.csv ) 
    [1] => Array ( [path] => data/example/example/ [filename] => file.csv ) 
    [2] => Array ( [path] => dexampleata/example// [filename] => file.csv ) ) 
[current_item:protected] => 
[created] => 0 
[updated] => 0 
[total:protected] => Array ( ) 
[progress:protected] => Array ( [fetching] => 1 [parsing] => 1 [processing] => 1 ) )
Run Code Online (Sandbox Code Playgroud)

我需要访问包含三个键的数组,以及一些后期处理的数据.

什么是抓住阵列的最好方法?

Fan*_*nis 10

如果您可以编辑该类,请将您关注的属性更改为public或为其编写getter:

function getItems() {
    return $this->items ;
}
Run Code Online (Sandbox Code Playgroud)

否则,如果您无法编辑类本身,则可以扩展它,因为您想要的属性受到保护,这意味着子类可以访问它们:

class YourClass extends ThatClass {

    public function getItems {
        //parent $items really
        return $this->items ;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后你需要创建一个YourClass实例而不是ThatClass,并从中获取items数组.

与您想要的任何其他受保护属性类似.