1

Is there a way to change the default functionality in Zend_Db fetchall() method, so that it doesn't return this:

[0] => 100000055944231
[1] => 100000089064543
[2] => 100000145893011
[3] => 100000160760965

but this:

[100000055944231]
[100000089064543]
[100000145893011]
[100000160760965]
Osvaldo Mercado
  • 960
  • 3
  • 13
  • 24
  • But isn't it the same? What exactly do you need? Series of strings? fetchAll always will return an array - you can set what this array will look like, but still it will always be an array – BartekR Jan 13 '12 at 20:26

3 Answers3

2

Although your question is actually flawed (noted by BartekR), I suppose you're trying to receive a simple array, instead of a multidimensional one.

You could do:

$results = $this->db->fetchAll($select);
$values = array_map(function($row) { return $row['column']; }, $results);

This will turn:

array(
    array('column' => 'test'),
    array('column' => 'test2'),
    array('column' => 'test3'),
);

into

array(
    'test',
    'test2',
    'test3'
);

(note; my example only works in PHP5.3+, if you're working with PHP5.2, you can define the function and use it by its name with array_map (e.g. array_map('methodName', $results))

Pieter
  • 1,764
  • 1
  • 12
  • 16
0

Further to Pieter's, I'd add the case where the rows are themselves arrays, and not just scalars; it's possible to nest the results, to as many fields as the query contains.

e.g. Here with two levels of nesting, respectively on field1 and field2.

$complex_results = array_map(function($row) { return array($row['field1'] => array($row['field2'] => $row)); }, $results);

As always, each row contains all fields, but $complex_results is indexed by field1, then field2 only.

Fabien Haddadi
  • 1,814
  • 17
  • 22
0

I'm looking for a similar solution, I'm trying to load a field returned by the fetchAll($select) as the array key.. Without looping through the entire resultset.

So:

$results = $this->db->fetchAll($select, <FIELDNAME_TO_MAKE_KEY_OF_RESULTS_ARRAY>);

results[<my fieldname>]->dbfield2;
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Mrmartin
  • 105
  • 1
  • 6