1

如何创建多维数组?目前这个查询返回给我这个:

Array
(
    [id] => 1
    [name] => Samsung galaxy S4 
    [homepage] => 1
)

但我希望我返回这样的数据:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Samsung galaxy S4 
            [homepage] => 1
        )

    [1] => Array
        (
            [id] => 3
            [name] => Iphone 5 
            [homepage] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => Samsung galaxy S3 
            [homepage] => 1
        )
)

模型:

<?php
namespace Application\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;

class News
{
    const TABLE = 'news';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->sql = new Sql($adapter);
    }

    public function getNews()
    {
        $select = $this->sql->select();
        $select->from(self::TABLE)
               ->where(array('homepage' => 1));
        $statement = $this->sql->prepareStatementForSqlObject($select);
        return $statement->execute()->current();        
    }
}

问题不在于查询,而是将其全部转化为多维数组。如何从数据库中创建一个数组。

4

1 回答 1

0

同意 Sam,只需从您的代码中删除 current(),您就会得到您想要的:

$result = $statement->execute();
$result = array_values(iterator_to_array($result));
return $result;
于 2013-06-07T05:51:43.933 回答