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

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.

If i understood correctly you need something like:




$key = 0;

foreach($array as $value):


  $arr[0][$key]['id']=> $value->id;

  $arr[0][$key]['name']=> $value->name;


endforeach;



Not tested, but it should work