3

In a production environment that uses the Yii1 framework and PHP 5.6.40, the array_column function is returning an empty array.

The array is a list of CActiveRecords from another CActiveRecord's HAS_MANY relation. On my local machine (PHP 7.1.23), the array_column function works as expected. Unless I misunderstand, the documentation says that array_column is available in 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'));

Expected results: Array ( [0] => 1 [1] => 2 [2] => 3 ) Actual results: Array ( ).

hutch90
  • 341
  • 3
  • 15
  • Note: was 7.0.0 they added the ability for the input parameter to be an array of objects. And that looks like an object ->. – ficuscr Jul 23 '19 at 17:44
  • It looks like that answers the question. array_column does not work as I expected in version 5.6.40. – hutch90 Jul 23 '19 at 19:12

1 Answers1

1

Version Description 7.0.0 Added the ability for the input parameter to be an array of objects.

wyzeman
  • 89
  • 4
  • Not a great answer. You might have instead joined me in voting it closed as a duplicate. – ficuscr Jul 23 '19 at 17:54
  • @wyzeman I appreciate the response. I have marked this as a duplicate linking to https://stackoverflow.com/questions/23335845/php-is-it-possible-to-use-array-column-with-an-array-of-objects – hutch90 Jul 23 '19 at 19:15
  • @ficuscr at the time I wrote my answer, you haven't wrote your comment yet. – wyzeman Jul 24 '19 at 15:00