Question

is it possible to fetch array from db, taking one column as array key and other column as array value?

My current code:

$table = new Zend_Db_Table('translations');

$where = $table->getAdapter()->quoteInto('lang = ?', $locale);      

$result = $table->fetchAll($where)->toArray();

Table structure:

id     key     lang     title
1      key1    en       Some english text
2      key2    de       Some german text

So after fetching an array I would like to get array that contains key value as a array key, and a title as a array key value.

Your help would be appreciated.

Was it helpful?

Solution

If you need pairs, it's better to do like this

$table = new Zend_Db_Table ('translations');
$db = $table->getAdapter();
$select = $table->select ()
    ->columns(array('key','title'))
    ->where ('lang = ?', $locale);
$result = $db->fetchPairs($select);

OTHER TIPS

I'm not sure what you mean, but let's try this:

    $table = new Zend_Db_Table ('translations');
    $query = $table->select ()
        ->where ('lang = ?', $locale);

    $results = $table->getAdapter ()
        ->fetchAll ($query, array (), Zend_Db::FETCH_GROUP);

    Zend_Debug::dump ($results);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top