Reputation: 159
My foreach loop takes $_POST data and using explode
separates an id number from a $key
so that I can use those id numbers in my db. Eventually I go on and create sub-arrays based on the id numbers
This works fine except I'm missing the last sub-array. I $_POST data that should turn into 16 subarrays, but I only get 15. When I var_dump the $_POST before the foreach, I show all 16. When I var_dump the variable defined by the explode after the foreach, but before the if{}
, I can see the 16th sub-array data minus one element, which is the id number.
So why would explode lose this last element? Limits don't seem to apply here, and strtok or preg_split don't seem to do the job I need done.
Incidentally, I tried modifying the count statement to be if(count($newgamearr)>0);
and I do get the 16th id and sub-array, but also a load of undefined offset: 1 errors and ultimately an 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 as $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 the var_dump output (from before the 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!"
}
Upvotes: 0
Views: 1578
Reputation: 4889
The first check for if($gamearr['gameid'])
won't match because the value is still null.
What you're doing here is delegating the adding into $mainarray
for the next iteration of the loop, and I presume your data would be getting out of sync in the process, matching the rest of the data with the subsequent game ID.
Move the $mainarr
add further down, where $gamearr['gameid']
is defined to the current $newgameID
:
if(count($newgameID) > 1) {
$gamearr = $template;
$gamearr['gameid'] = $newgameID[1];
$gamearr['userid'] = $userid;
if($gamearr['gameid']) {
$mainarray[] = $gamearr;
}
continue;
}
Further, after this move, since you have already verified that there is a $newgameID
, the if($gamearr['gameid'])
is redundant, so you can snip that condition out.
==== EDIT: Another Way... ====
You could do something like this instead to make it shorter:
foreach($_POST as $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 you want to add that in too.
$mainarray[$id][$var] = $post_data;
}
}
Would land you with a $mainarray
with a more tidy layout, and you'd also have no need for the $template
since it's just the keys in lower-case. :)
Upvotes: 1