HELPME
HELPME

Reputation: 754

array_unshift command doesn't work properly in yii2

I have a problem here. I have a drop-down list with a users and when I click on any user, it shows a clients which are assigned to it.

Now I've added Unassigned clients selection into the drop-down top to show which clients are unassigned. I've added it with a array_unshift() command, but now in some existing users (not all of it) it's not showing any clients, but these are assigned in the database.

When I remove array_unshift, the clients shows properly, but then there are no extra selection.

I'm thinking that there are something wrong with the array_unshift(), but not sure..

Here is my select function:

public static function getUsers()
{
    $query = self::find()
        ->select(['id', "CONCAT(name, ' ', surname, ' (', email, ')') as name"]);

    $users = ArrayHelper::map($query->asArray()->all(), 'id', 'name');

    array_unshift($users, 'Test'));

    return $users;
}

Upvotes: 0

Views: 72

Answers (1)

Bizley
Bizley

Reputation: 18021

From PHP docs:

All numerical array keys will be modified to start counting from zero while literal keys won't be changed.

Use +:

public static function getUnmappedUsersList()
{
    $query = self::find()
        ->select(['id', "CONCAT(name, ' ', surname, ' (', email, ')') as name"]);

    return [Yii::t('app', 'ADMIN_PANEL_MIGRATE_UNASSIGNED_CLIENTS')] 
        + ArrayHelper::map($query->asArray()->all(), 'id', 'name');
}

Upvotes: 3

Related Questions