phparraysreorganize

How can I change my array sorting to like that


This is my array which I get on my database:

Array (

 [0] => Array 
    ( 
        [ID] => 4 
        [USERNAME] => John
    ) 

[1] => Array 
    ( 
        [ID] => 7 
        [USERNAME] => Ted 
    ) 

)

But I want to like this:

Array 
( 
   [4] => John 
   [7] => Ted 
) 

I tried to do like I want by using array_merge, array_push etc. But I couldn't.


Solution

  • I'd use a simple foreach.

    $users = array();
    
    foreach ($results as $user) {
      $users[$user['ID']] = $user['USERNAME'];
    }