Return all of the rows in the result as an array.
// Indexed array of all rows $rows = $result->as_array();
// Associative array of rows by "id" $rows = $result->as_array('id');
// Associative array of rows, "id" => "name" $rows = $result->as_array('id', 'name');
array Module_Database_Result::as_array( [ string $key = null , string $value = null ] )
参数列表
参数 类型 描述 默认值 $key
string
Column for associative keys null $value
string
Column for values null
array
public function as_array($key = null, $value = null)
{
$results = array();
if ( $key === null && $value === null )
{
// Indexed rows
foreach ( $this as $row )
{
$results[] = $row;
}
}
elseif ( $key === null )
{
// Indexed columns
if ( $this->_as_object )
{
foreach ( $this as $row )
{
$results[] = $row->$value;
}
}
else
{
foreach ( $this as $row )
{
$results[] = $row[$value];
}
}
}
elseif ( $value === null )
{
// Associative rows
if ( $this->_as_object )
{
foreach ( $this as $row )
{
$results[$row->$key] = $row;
}
}
else
{
foreach ( $this as $row )
{
$results[$row[$key]] = $row;
}
}
}
else
{
// Associative columns
if ( $this->_as_object )
{
foreach ( $this as $row )
{
$results[$row->$key] = $row->$value;
}
}
else
{
foreach ( $this as $row )
{
$results[$row[$key]] = $row[$value];
}
}
}
$this->rewind();
return $results;
}