/**
* Fetches results from the database with each row keyed according to preference.
* The 'key' parameter provides the column name with which to key the result.
* For example, calling fetchAllKeyed('SELECT item_id, title, date FROM table', 'item_id')
* would result in an array keyed by item_id:
* [$itemId] => array('item_id' => $itemId, 'title' => $title, 'date' => $date)
*
* Note that the specified key must exist in the query result, or it will be ignored.
*
* @param string SQL to execute
* @param string Column with which to key the results array
* @param mixed Parameters for the SQL
*
* @return array
*/
public function fetchAllKeyed($sql, $key, $bind = array())
{
$results = array();
$i = 0;
$stmt = $this->_getDb()->query($sql, $bind, Zend_Db::FETCH_ASSOC);
while ($row = $stmt->fetch())
{
$i++;
$results[(isset($row[$key]) ? $row[$key] : $i)] = $row;
}
return $results;
}
Над фрагментом кода взят из системы xenforo.пытается понять: извлекает результаты из базы данных с каждой строкой в соответствии с предпочтением
Вопрос:
Хотя есть комментарии по этой функции, до сих пор не понимаю, каким образом «ключ» параметров работы? В комментариях сказано:
* For example, calling fetchAllKeyed('SELECT item_id, title, date FROM table', 'item_id')
* would result in an array keyed by item_id:
* [$itemId] => array('item_id' => $itemId, 'title' => $title, 'date' => $date)
so if the table looks like this:
item_id title date
1 book 2000
2 car 2000
3 laptop 2001
...
так, что результат будет?
так, если я хочу, чтобы получить доступ к элементу: Название: автомобиль, я должен сделать [2] [ «название»], не так ли? – user2507818
@ user2507818 Да. :) –