Optional array wrapper

I am stumbling over something annoying and hope you can shed some light. I am generating array via PHP MySQL query. My WHILE statement looks like this:

$model[constraints][d][] = array($row['Node'] => array("max" => $row['dem'], "min"=> $row['dem']));

      

The problem is that every new array added to $ model [constraints] [d] ends up in the array. See screenshot below:

enter image description here

I don’t want a "0" around Norway. I would like to be able to access my values ​​like this:

$model[constraints][s][Norway][max]

      

Right now, the only way to access this value is by doing the following:

$model[constraints][s][0][Norway][max]

      

How do I modify my while statement to get the array I want? Thank you for your time.

+3


source to share


1 answer


Before the loop, you can do:

$model[constraints][d] = array();

      

And then just change the statement inside the loop to:



$model[constraints][d] += array($row['Node'] => array("max" => $row['dem'], "min"=> $row['dem']));

      

Here's a live demo

+1


source







All Articles