Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
DB::select, how to return one dimensional array for more then one row fields
  • Hi I'm trying to get from DB table instead of 2 dimensions only 1 dimensional array without making additional foreach loop.
    I want to get such array: <code>
    Array
    (
    [id] => 1234
    [username] => root
    [group] => 100
    [email] => tester@tester.com
    )
    </code>
    What I'am always getting is 2 dimensions: <code>
    Array
    (
    [0] => Array
    (
    [id] => 1234
    [username] => root
    [group] => 100
    [email] => tester@tester.com
    )
    )
    </code>
    My query:
    <code>
    $aUser = DB::select('id', 'username', 'group', 'email')->from('users')->where('id', '=', 1234)->execute()->as_array(null, null);
    </code>
    Can you help me please to get that 1 dimensional array back from the query?
    Am I missing something? Thank you.
    Jozef
  • Thank you daGrevis.
    I saw that forum already.
    I need someting what writes Dan there, but for more than one field, not only output for e.g. 'username': This is what I have got: $query = \DB::select( 'id', 'username', 'group', 'email' )
    ->from( 'users' )
    ->where('id', '=', 1234 )
    ->execute()
    ->as_array(null, 'username');
    print_r($query); Array
    (
    [0] => root
    )
    But I would like to get: Array
    (
    [id] => 1234
    [username] => root
    [group] => 100
    [email] => tester@tester.com
    )
  • This should give you the result you desire.
    $query = \DB::select( 'id', 'username', 'group', 'email' )
    ->from( 'users' )
    ->where('id', '=', 1234 )
    ->execute()
    ->as_array();
    
    $query = $query[0];
    
    print_r($query);
    
    array (
      'id' => '1234',
      'username' => 'paulboco',
      'group' => '100',
      'email' => 'paulboco@foo.bar',
    )
    
  • Exactly that way via
    $query = $query[0];
    I did it. ;-)
    But what I am trying to get is the same result directly from the select query.
  • $query = \DB::select( 'id', 'username', 'group', 'email' )
    ->from( 'users' )
    ->where('id', '=', 1234 )
    ->execute()
    ->current();
    
  • Thank you Santiago!
    ->current();
    ist the method I was looking for.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion