Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnecessary array wrapper

Tags:

arrays

php

mysql

I am stumbling on something annoying and hope you can shed some light. I am generating an array via PHP MySQL query. My WHILE statement is as follows:

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

The problem is that each new array added to $model[constraints][d] gets enclosed by an array. See screenshot below:

enter image description here

I don't want there to be array "0" around Norway. I would like to be able to access my values as follows:

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

Right now, the only way I can access that value is by doing the following:

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

How should I amend my while statement to get the desired array? Thank you for your time.

like image 791
Noobster Avatar asked Sep 14 '26 18:09

Noobster


1 Answers

Before the loop you could 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

like image 100
JSelser Avatar answered Sep 17 '26 08:09

JSelser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!