php - Adding array elements with a for(reach) loop -


(first bit/lot of context @ bottom question)

i writing api returns planning amongst other things. response in json , should followed:

"planning":   [   {     "name": "overview",     "dates":       [       {         "date": "yyyy-mm-dd",         "division1": "type",         "division2": "type"       },       {         "date": "yyyy-mm-dd",         "division1": "type",         "division2": "type"       },       ...       ]   },   {     "name": "division1",     "dates":       [       {         "date": "yyyy-mm-dd",         "type": "type",         "description": "type"       },       ...       ]   },   ... 

there standard blok "name": "overview" returned, , each division requester part of, block "name":"divisionname" added response.

the issue have amount or names of divisions aren't set in stone. there can more or less depending on deployment. cover wrote following code:

<?php     ...     $stmt = $conn->prepare("select `iddivision` division;");     $stmt->execute();     $stmt->bind_result($iddivision);     while($stmt->fetch()){         $divisions[] = $iddivision;         $$iddivision = array();     }     ... ?> 

this should create array each division array name being id of division (correct?).

then planning data db store in multiple arrays later use response building:

<?php $stmt->bind_result($type, $date, $iddivision, $day, $description, $note); while($stmt->fetch()){     if(checkarray($date, $arr_date) != true){         array_push($arr_date, $date);         array_push($arr_day, $day);     }     array_push($$iddivision, $type); //this should push correct array. } ?> 

at end want combine respons ofcourse (this lost):

<?php for($i = 0; $i <= count($arr_date); $i++){     $planning[0]['dates'][] = array(         "date" => $arr_date[$i],             // how add every division "arrayname" => "$type" here?         ); } ?> 

as in comment above, don't know how add key:value each division found dynamically becomes:

<?php for($i = 0; $i <= count($arr_date); $i++){     $planning[0]['dates'][] = array(         "date" => $arr_date[$i],         "division1" => $value,         "division2" => $value,         "division3" => $value,                     // , on every division         ); } ?> 

is there way or should go doing different way? feel should/could use $divisions array.

try out this.

for($i = 0; $i <= count($arr_date); $i++){     $planning[0]['dates'][$i]["date"] = $arr_date[$i];       for($j=0;$j<count($division);$j++){        $planning[0]['dates'][$i]["division$j"] = $division[$j];   } } ?> 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -