此问题在此处已有答案:
PHP. Is it possible to use array_column with an array of objects(5个答案)
三年前就关门了。
在使用Yii1框架和PHP 5.6.40的生产环境中,array_column函数返回的是一个空数组。
这个数组是一个CActiveRecord的列表,它来自另一个CActiveRecord的HAS_MANY关系。在我的本地机器(PHP 7.1.23)上,array_column函数可以正常工作。除非我误解了,否则documentation说明array_column在PHP 5.6.40中是可用的。
/**
* This is the model class for table 'my_active_record'.
*
* The following are the available columns in table 'my_active_record':
* @property integer $id
*
* The following are the available model relations:
* @property RelatedActiveRecord[] $relatedActiveRecords
*/
class MyActiveRecord extends CActiveRecord
{
public function relations()
{
return array(
'relatedActiveRecords' => array(self::HAS_MANY, 'related_active_records', 'my_active_record_id')
);
}
}
/**
* This is the model class for table 'related_active_record'.
*
* The following are the available columns in table 'related_active_record':
* @property integer $id
* @property integer $my_active_record_id
*
* The following are the available model relations:
* @property MyActiveRecord $myActiveRecord
*/
class MyActiveRecord extends CActiveRecord
{
public function relations()
{
return array(
'myActiveRecord' => array(self::BELONGS_TO, 'my_active_record', 'my_active_record_id')
);
}
}
$myActiveRecord = new MyActiveRecord();
print_r(array_column($myActiveRecord->relatedActiveRecords, 'id'));
预期结果:Array ( [0] => 1 [1] => 2 [2] => 3 )
实际结果:Array ( ).
个
1条答案
按热度按时间dy1byipe1#
版本描述7.0.0增加了输入参数成为对象数组的能力。