phpzend-dbfetchall

Zend_Db fetchAll() to return values as keys, not as key => value


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]

Solution

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