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 ( ).

2
  • 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 ->. Commented Jul 23, 2019 at 17:44
  • It looks like that answers the question. array_column does not work as I expected in version 5.6.40. Commented Jul 23, 2019 at 19:12

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Not a great answer. You might have instead joined me in voting it closed as a duplicate.
@wyzeman I appreciate the response. I have marked this as a duplicate linking to stackoverflow.com/questions/23335845/…
@ficuscr at the time I wrote my answer, you haven't wrote your comment yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.