c.k c.k - 3 years ago 80
PHP Question

Set some array value as key of an array

I have have this line of code:

foreach($users_array as $value){
echo "<pre>";
print_r($value);
}


Which display this kind of array.

Array
(
[auto_id] => 45
[id] => 20151116
[name] => Peter 2
[department] =>
[position] =>
[rate] => 300
[date_added] => 2017-07-26 09:31:44
)
Array
(
[auto_id] => 80
[id] => 20160410
[name] => John 2
[department] =>
[position] =>
[rate] => 400
[date_added] => 2017-07-26 09:31:48
)


Now what I wanted to do is to make the id of employee to be the key of an array and make them as one multi-dimensional array.

Example output should be like this:

Array
(
[20151116] => Array
(
[auto_id] => 45
[id] => 20151116
[name] => Peter 2
[department] =>
[position] =>
[rate] => 300
[date_added] => 2017-07-26 09:31:44
)

[20160410] => Array
(
[auto_id] => 80
[id] => 20160410
[name] => John 2
[department] =>
[position] =>
[rate] => 400
[date_added] => 2017-07-26 09:31:48
)

)


Any help is appreciated. Thank you.

Answer Source

Here you need to change

  $final = array();
  foreach($users_array as $value){    
      $final[$value["id"]] = $value;                 
  }
      echo "<pre>";
      print_r($final);  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download