PHP 5.6中array_column返回空数组

3
在使用Yii1框架和PHP 5.6.40的生产环境中,array_column函数返回一个空数组。
该数组是另一个CActiveRecord的HAS_MANY关系中的CActiveRecords列表。在我的本地机器上(PHP 7.1.23),array_column函数正常工作。除非我理解有误,否则文档说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'));

预期结果: 数组 ( [0] => 1 [1] => 2 [2] => 3 ) 实际结果: 数组 ( )。


注意:在7.0.0版本中,他们添加了输入参数可以是对象数组的功能。而这看起来像一个对象->。 - ficuscr
看起来这回答了问题。在5.6.40版本中,array_column的工作方式并不像我预期的那样。 - hutch90
1个回答

1

版本说明 7.0.0 新增了输入参数可以是对象数组的功能。


不是一个很好的答案。你本可以和我一起投票将其关闭为重复问题。 - ficuscr
@wyzeman,感谢您的回复。我已将此标记为重复,并链接到https://dev59.com/bWAg5IYBdhLWcg3wq8aU。 - hutch90
@ficuscr 在我回答的时候,你还没有写下你的评论。 - wyzeman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接