How to make associative array using PHP for loop to use in Yii 2 array map()? -


i make associative array using php loop use in yii2 map() method.

the array in bellow format-

$listarray = [   ['id' => '1', 'name' => 'peter/5'],   ['id' => '2', 'name' => 'john/7'],   ['id' => '3', 'name' => 'kamel/9'], ]; 

the id , name changed through each iteration of loop. here, name hold customized value after calculation inside loop.

finally, list used in map() method following

$listdata=arrayhelper::map($listarray,'id','name'); 

i can use map() method directly after using active record find list array , use in map() method. not give me way use custom value 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 great me.

thanks in advance.

i'm assuming want query activerecord data transfer data simple array.

$listdata = [];  $listarray = userlist::find()     ->where(['status' => 1])     ->orderby('name')     ->all();  foreach($listarray $user){     $customname = $user->name . $this->somecalculation();     $listdata[] = ["id" => $user->id, "name" => $customname];  } 

or use arrayhelper class 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         },     ], ]); 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -