1

I would like to make an associative array using PHP for loop to use in Yii2 map() method.

The array will look like in bellow format-

$listArray = [
  ['id' => '1', 'name' => 'Peter/5'],
  ['id' => '2', 'name' => 'John/7'],
  ['id' => '3', 'name' => 'Kamel/9'],
];

The id and name will be changed through each iteration of the loop. Here, the name will always hold customized value after some calculation inside the loop.

Finally, the list will be used in map() method like as following

$listData=ArrayHelper::map($listArray,'id','name');

I can use map() method directly after using the Active Record to find the list array and then use that in map() method. But it does not a give me way to use custom value for the name attribute.

$listArray = UserList::find()
        ->where(['status' => 1])
        ->orderBy('name')
        ->all();

$listData=ArrayHelper::map($listArray,'id','name');

How can achieve this? Direct source code example would be really great for me.

Thanks in advance.

3
  • If you can iterate through the returned rows, you could easily build out an associative array within the for each loop. I'm not overly familiar with Yii2 but I imagine the principles are the same and you could just use native PHP for this. Commented Jul 21, 2015 at 20:40
  • If you please help to write the code to make the associative array that would be really helpful for me. I could write for PHP generic array with () or {} but not for using [ [], [] ] this way. Thanks. Commented Jul 21, 2015 at 23:03
  • @xerxes333 answered below along the lines of what I was talking about. Commented Jul 22, 2015 at 12:58

3 Answers 3

3

I'm assuming you want to query an ActiveRecord for data then transfer the data into a simple array.

$listData = [];

$listArray = UserList::find()
    ->where(['status' => 1])
    ->orderBy('name')
    ->all();

foreach($listArray as $user){
    $customName = $user->name . $this->someCalculation();
    $listData[] = ["id" => $user->id, "name" => $customName]; 
}

Or you could use the ArrayHelper class like this:

$listArray = UserList::find()
    ->where(['status' => 1])
    ->orderBy('name')
    ->all();

$listData = ArrayHelper::toArray($listArray , [
    'app\models\UserList' => [
        'id',
        'name' => function ($listArray ) {
            return $listArray->word . strlen($listArray->word);  // custom code here
        },
    ],
]);
Sign up to request clarification or add additional context in comments.

Comments

1

I think the preferred way of doing this by defining custom calculation rule in UserList model as:

public function getCustomRuleForUser(){
  // Do what ever you want to do with your user name.
  return $this->name.'Your custom rule for name';

}

And use as:

  $userList = UserList::find()->all(); 
  $listData=ArrayHelper::map($userList,'id','customRuleForUser');

Now, you have your custom rule for username list in $listData.

Comments

0
$model_userprofile = UserProfile::find()->where(['user_id' => Yii::$app->user->id])->one();

        $model_userprofile1 = UserProfile::find()
            ->select('user_id')
            ->where(['group_id' => $model_userprofile->group_id])->all();

        $listData = [];

        foreach($model_userprofile1 as $user){
            $id = $user->user_id;
            $listData[] = ["id" => $id];
        }


        $dataProvider = new ActiveDataProvider
        ([
            'query' => User::find()
               ->select('id,username,email')
               ->Where(['id' => $listData])
               ->orderBy(['id' => SORT_DESC]),
            'pagination' => ['pagesize' => 15]]);

        return $this->render('index',['dataProvider'=> $dataProvider]);

Comments

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.