1

There is a comment on php.org at fetchAll page.

You might find yourself wanting to use FETCH_GROUP and FETCH_ASSOC at the same time, to get your table's primary key as the array key:

// $stmt is some query like "SELECT rowid, username, comment" 
$results = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);  
// It does work, but not as you might expect:  
$results = array(  
  1234 => array(0 => array('username' => 'abc', 'comment' => '[...]')),  
  1235 => array(0 => array('username' => 'def', 'comment' => '[...]')), );

but you can at least strip the useless numbered array out easily:

$results = array_map('reset', $results);

The code functions as expected and everything is accomplished with just line:

array_map('reset', $results);

After reading documentation array_map and reset function I don't really understand how the result is produced when both are combined in a single line.

Is it a safe solution? Would you recommend this kind of one-line or is it a side effect and should not be used, i.e. should I write a plain old loop to produce the same result?

Single line solution combining two standard functions is a very attractive solution to me. I just want to make sure there are no surprises.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Sergei G
  • 1,561
  • 3
  • 18
  • 26

2 Answers2

3

The trick here is that reset in this very case is equal (suggest we are iterating through $results in array_map) to a $results[$i][0]. reset, besides internal pointer rewind, returns the first element of the passed array, as simple as that.

As a side note I would suggest achieve this behavior with PDO::FETCH_CLASS implementing ArrayAccess Interface.

Evgeniy Chekan
  • 2,615
  • 1
  • 15
  • 23
-1

Yes, it is safe. array_map() replaces an array member with callback function's result and reset() returns first member of array.
So,

array(0 => array('username' => 'abc', 'comment' => '[...]'))

is replaced with

array('username' => 'abc', 'comment' => '[...]')

as reset() returns this very line out of the former one.

But honestly, this is a kind of problem which never existed to me.
I don't understand what is so attractive in the idea of making one-liners using API functions only, producing whatever mind-breaking statements to abide one rule - "no user-defined functions at any cost".
I can make whatever function of my own, and it will be shorter than any of these code twists when called, yet have plain and readable syntax in body, without scratching your head both at writing and reading, without asking yourself a question "is it safe?".

So, a real one-liners would be my safe and convenient database access library's getInd family functions (which allows any field as a key, not primary index only):

$data = $db->getInd("id", "SELECT * FROM table WHERE category=?i", $cat);
$dic  = $db->getIndCol("name", "SELECT name, id FROM cities");

you may notice that these are real one-liners, doing all the dirty and repetitive job inside, including query execution, parameter binding and stuff.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345