0

I have a problem with the implementation of a dynamic 2 dimensional array. This is the way I am doing this:

$array_ = array();
$values = array();

foreach ($queryresult as $row){
    array_push($value, $row['id'];
    array_push($value, $row['title'];
    array_push($array_, $value);
}  
var_dump(#array_);

The problem is that my query returns 3 rows, but my result is as follows:-

Array ([0] => Array ([0] => 44 [1] => Game of Thrones))

Anyone have any idea what I am doing wrong?

1
  • $array_ = new Array(); that's not valid PHP syntax. Remove "new", arrays are not objects. Commented Feb 13, 2013 at 14:28

2 Answers 2

8
$array_ = Array();

foreach ($queryresult as $row){
    $values = Array();

    array_push($values, $row['id'];
    array_push($values, $row['title'];
    array_push($array_, $values);
}  
var_dump($array_);

or even simpler

$array_ = Array();

foreach ($queryresult as $row){
    array_push($array_, 
        array(
            $row['id'],
            $row['title']
        )
    );
}  
var_dump($array_);
Sign up to request clarification or add additional context in comments.

Comments

2

Much cleaner.

$array_ = [];

foreach ($queryresult as $row){
   $array_[] = [$row['id'], $row['title']];
}  

var_dump($array_);

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.