小编Eld*_*din的帖子

PHP.是否可以将array_column与对象数组一起使用

是否可以传入array_column一个对象数组?
我已经实现了ArrayAccess接口,但它没有效果.
我应该再实施一个吗?

class Foo implements ArrayAccess {

    public $Id, $Title;

    public function offsetExists($offset)
    {
        return isset($this->{$offset});
    }    

    public function offsetGet($offset)
    {
        return $this->{$offset};
    }

    public function offsetSet($offset, $value)
    {
        $this->{$offset} = $value;
    }

    public function offsetUnset($offset)
    {
        unset($this->{$offset});
    }
}

$object = new \Foo();
$object->Id = 1;
$object->Title = 'Test';

$records = array(
    $object, 
    array(
        'Id' => 2,
        'Title' => 'John'
    )
);

var_dump(array_column($records, 'Title')); // array (size=1) 0 => string 'John' (length=4)
Run Code Online (Sandbox Code Playgroud)

php

54
推荐指数
2
解决办法
3万
查看次数

标签 统计

php ×1