Quoc Huy Quoc Huy - 2 months ago 8
MySQL Question

Format in array PHP

I have an array as output from mysql database query as below

Array
(
[0] => Array
(
[id_user] => 1
[name_user] => Thomas
)

[1] => Array
(
[id_user] => 3
[name_user] => David
)

)


Then I would like format in array with example below

array('1'=>'Thomas','3'=>'David');


Please help me . Thanks All.

Answer

You need a foreach loop.

$res = []; // declare an array
foreach($array as $record){
  $res[$record['id_user']] = $record['name_user'];
}
Comments