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]
1
  • 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 Commented Jan 13, 2012 at 20:26

3 Answers 3

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))

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.