PHP foreach array missing last element after explode, count functions -


my foreach loop takes $_post data , using explodeseparates id number $key can use id numbers in db. go on , create sub-arrays based on id numbers

this works fine except i'm missing last sub-array. $_post data should turn 16 subarrays, 15. when var_dump $_post before foreach, show 16. when var_dump variable defined explode after foreach, before if{}, can see 16th sub-array data minus 1 element, id number.

so why explode lose last element? limits don't seem apply here, , strtok or preg_split don't seem job need done.

incidentally, tried modifying count statement if(count($newgamearr)>0); , 16th id , sub-array, load of undefined offset: 1 errors , empty data set.

public function pickarray() {  $template  = [         'gameid'=> '',         'ats'=>'',         'winner'=>'',                    'ou'=>'',         'lck'=>'',         'userid'=>'',         ];      $gamearr = [];      $mainarray = [];      $userid = $this->session->userdata('user_id')->id;        $gamearr = [];     $mainarray = [];             $gamearr['gameid'] = null;    foreach($_post $key=>$post_data){     $newgameid = explode('gameid',$key);  if(count($newgameid)>1) {          if($gamearr['gameid']) {          $mainarray[] = $gamearr;         }                 $gamearr = $template;                 $gamearr['gameid'] = $newgameid[1];      $gamearr['userid'] = $userid;      continue;}         $newats = explode('ats',$key);        if(count($newats)>1) {     $gamearr['ats'] = $post_data;     continue;}        $newwinner = explode('winner',$key);     if(count($newwinner)>1) {        $gamearr['winner'] = $post_data;     continue;}         $newou = explode('ou',$key);     if(count($newou)>1) {        $gamearr['ou'] = $post_data;     continue;}       $newlock = explode('lck',$key);     if(count($newlock)>1) {        $gamearr['lck'] = $post_data;     continue;}     } 

here's var_dump output (from before foreach):

array(55) { ["gameid1"]=> string(1) "1" ["ats1"]=> string(3) "sea" ["ou1"]=> string(4) "over" ["winner1"]=> string(3) "sea" ["lck1"]=> string(0) "" ["gameid2"]=> string(1) "2" ["ats2"]=> string(2) "no" ["ou2"]=> string(4) "over" ["winner2"]=> string(2) "no" ["lck2"]=> string(0) "" ["gameid3"]=> string(1) "3" ["ats3"]=> string(3) "stl" ["ou3"]=> string(4) "over" ["winner3"]=> string(3) "stl" ["lck3"]=> string(0) "" ["gameid4"]=> string(1) "4" ["winner4"]=> string(0) "" ["lck4"]=> string(0) "" ["gameid5"]=> string(1) "5" ["winner5"]=> string(0) "" ["lck5"]=> string(0) "" ["gameid6"]=> string(1) "6" ["winner6"]=> string(0) "" ["lck6"]=> string(0) "" ["gameid7"]=> string(1) "7" ["winner7"]=> string(0) "" ["lck7"]=> string(0) "" ["gameid8"]=> string(1) "8" ["winner8"]=> string(0) "" ["lck8"]=> string(0) "" ["gameid9"]=> string(1) "9" ["winner9"]=> string(0) "" ["lck9"]=> string(0) "" ["gameid10"]=> string(2) "10" ["winner10"]=> string(0) "" ["lck10"]=> string(0) "" ["gameid11"]=> string(2) "11" ["winner11"]=> string(0) "" ["lck11"]=> string(0) "" ["gameid12"]=> string(2) "12" ["winner12"]=> string(0) "" ["lck12"]=> string(0) "" ["gameid13"]=> string(2) "13" ["winner13"]=> string(0) "" ["lck13"]=> string(0) "" ["gameid14"]=> string(2) "14" ["winner14"]=> string(0) "" ["lck14"]=> string(0) "" ["gameid15"]=> string(2) "15" ["winner15"]=> string(0) "" ["lck15"]=> string(0) "" ["gameid16"]=> string(2) "16" ["winner16"]=> string(0) "" ["lck16"]=> string(0) "" ["submitpicks"]=> string(13) "submit picks!" }

the first check if($gamearr['gameid']) won't match because value still null.

what you're doing here delegating adding $mainarray next iteration of loop, , presume data getting out of sync in process, matching rest of data subsequent game id.

move $mainarr add further down, $gamearr['gameid'] defined current $newgameid:

if(count($newgameid) > 1) {     $gamearr = $template;                 $gamearr['gameid'] = $newgameid[1];     $gamearr['userid'] = $userid;      if($gamearr['gameid']) {         $mainarray[] = $gamearr;     }     continue; } 

further, after move, since have verified there $newgameid, if($gamearr['gameid']) redundant, can snip condition out.

==== edit: way... ====

you instead make shorter:

foreach($_post $key=>$post_data) {     preg_match("#([a-za-z]+)([0-9]+)#", $key, $data);      if (isset($data[2])) {         $id = $data[2];         $var = strtolower($data[1]);          if ($var == 'gameid') continue; // unless want add in too.          $mainarray[$id][$var] = $post_data;     } } 

would land $mainarray more tidy layout, , you'd have no need $template since it's keys in lower-case. :)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -