Loop problem...in prepared statement

I am using this loop to bring more than one row from a table:

  while ($stmt->fetch()) {
      $holidays[]= ['holid_date'=>$holid_date,'holid_name'=>$name];
    }

the problem with the above is that it creates a multi-dimensional array…the key names represent table cols.
Can the multidimensional array be avoided…is it possible?By altering the syntax.

What is it that you are trying to achieve?
Do you want an associative array where the results represent key/value?

Like this?:

while ($stmt->fetch()) {
    $holidays[$holid_date] = $name;
}

{Disclaimer: it has been years since I have actually written PHP code. This is ‘from memory’}

this is less a question whether it is possible, but what you want the final array to look like.

I just want it to look like a non-multidimensional/associative array…parkinT 's code is the solution…I just want to do something more before closing the topic

in PDO you’d use the PDO::FETCH_KEY_PAIR option for that. which reduces the code to

$holiddays = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

(note that this is a PDO-only solution, it won’t work with MySQLi!)

That is great to hear.
Thanks for making an old man feel good. As I said, it has been quite a long time since I did anything serious with PHP. My memory is not quite as bad as I thought!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.