1

how can I create a multidimensional array? currently this query returns me this:

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

but I wish that I returned the data like this:

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

Model:

<?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();        
    }
}

The problem is not in the query but in turn it all into multidimensional array. How do I create an array so from the database.

Carol Casta
  • 75
  • 1
  • 9
  • 3
    You return `->current()` that selects the FIRST entry of your Multi-Dimensional array, which would be returned by `$statement->execute()` - cut the `->current()` and you got what you want – Sam Jun 03 '13 at 16:42

1 Answers1

0

agree with Sam, just remove the current() form your code, you will get what you desired like:

$result = $statement->execute();
$result = array_values(iterator_to_array($result));
return $result;
graphicmist
  • 71
  • 3
  • 15