是否可以通过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)
PHP 5
array_column
不适用于对象数组。使用array_map
反而:
$titles = array_map(function($e) {
return is_object($e) ? $e->Title : $e['Title'];
}, $records);
PHP 7
array_column()
该函数现在支持对象数组以及
二维数组。仅考虑公共属性,并且
使用的对象__get()
对于动态属性还必须
实施__isset()
.
See https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L629-
谢谢Bell为了提示!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)