I have a function that gets users from a MySQL database, the results rows can be 1 or multiple rows.
What annoys me is that when i’m only looking to get 1 result from the db, it returns me a multi-dimentional array like this:
$result = array([0]=>array('foo'=>'bar'))
And makes me write nasty code like:
$e = $result[0]['foo'] // instead of $result['foo']
I’m pretty sure that many people came across this situation, i thought it would be cool if i can check if there is only one row returned and then append to $result an ungrouped version of it so i can use it when i’m looking for only 1 row. so it’d be like this:
$result = array(
[0] => array('foo'=>'bar'), // will keep the multi-dimentional version
'foo' => 'bar' // and append the ungrouped version of $result here
);
How to do that?
1 Answer