0

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;
}

1 Answer 1

3

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');
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works. Thank you. Could you also please explain the sentence you've written about all numerical keys...? I can't get it. I'm trying to var_dump() mine array_unshift and your return, but the result gives me exact same array. So what is different?
It can not be the same array. ArrayHelper::map generates something like ['12' => 'aaa', '34' => 'bbb', '36' => 'ccc'] when you use array_unshift it is being remapped to [0 => 'unshifted element', 1 => 'aaa', 2 => 'bbb', 3 => 'ccc'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.