Question

Hey, I'm using FuelPHP and doing like this...

$query =
  \DB::select( 'username' )
    ->from( 'users' )
    ->execute()
    ->as_array();

I'm getting array as shown below.

Array
(
  [0] => Array
  (
    [username] => daGrevis
  )

  [1] => Array
  (
    [username] => whatever
  )

  [2] => Array
  (
    [username] => foobar
  )

)

It's definitely not what I need. Here's example of "ideal array" for me:

Array
(
  [0] => daGrevis
  [1] => whatever
  [2] => foobar
)

So how can I get "ideal array"? Maybe I do something wrong in the query... if no... how can I convert array #1 to array #2? Using loops maybe? Or there is built-in function? I'm confused.

Was it helpful?

Solution

You can do this with Fuel natively:

$query = \DB::select( 'username' )
           ->from( 'users' )
           ->execute()
           ->as_array(null, 'username');

This will return exactly what you want.

OTHER TIPS

Yep, a foreach loop should do the trick:

$new_array = array();
foreach($query as $result_num => $sub_array)
{
    $new_array[$result_num] = $sub_array[username];
}

Do a foreach loop to convert it, something like this:

$arr2 = array();
foreach ($arr1 as &$value) {
array_push($arr2, $value[username]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top