【问题标题】:array_column returning empty array in PHP 5.6 [duplicate]array_column 在 PHP 5.6 中返回空数组 [重复]
【发布时间】:2019-12-01 19:23:00
【问题描述】:

在使用 Yii1 框架和 PHP 5.6.40 的生产环境中,array_column 函数返回一个空数组。

该数组是来自另一个 CActiveRecord 的 HAS_MANY 关系的 CActiveRecord 列表。在我的本地机器 (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 ( ).

【问题讨论】:

  • 注意:在 7.0.0 中,他们添加了输入参数成为对象数组的功能。这看起来像一个对象 ->.
  • 看起来回答了这个问题。 array_column 在 5.6.40 版本中无法正常工作。

标签: php yii php-5.6 yii1.x array-column


【解决方案1】:

版本说明 7.0.0 新增输入参数为对象数组的功能。

【讨论】:

  • 不是一个很好的答案。相反,您可能已经加入我的投票,将其作为重复关闭。
  • @wyzeman 我感谢您的回复。我已将此标记为重复链接到 stackoverflow.com/questions/23335845/…
  • @ficuscr 在我写答案时,您还没有写评论。
猜你喜欢
  • 2020-11-25
  • 2016-06-29
  • 2017-04-20
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2017-11-01
相关资源
最近更新 更多