0

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.

2
  • Not unique. The same key can be in different languages. Commented Nov 30, 2012 at 11:47
  • Just a small note. Similar, but more general function is ->fetchAssoc(). Commented May 23, 2013 at 19:27

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.