Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My foreach loop takes $_POST data and using explodeseparates 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!" }

share|improve this question
    
Are you sure you don't just look at like the first 15 elements in your db and the 16 element just don't get displayed? – Rizier123 May 30 '15 at 20:59
    
I'm definitely seeing all 16 - as mentioned, I var_dump($_POST) before the foreach and they're all there – edoras May 30 '15 at 21:02
    
mind if you can post the 'var_dump(yourarray)' here? – Zaffar Saffee May 30 '15 at 21:05
    
u might have the starting $gamearr['gameid'] == 0 so ur loop skip it. It's the easier explanation of this misterious missing element. – Falt4rm May 30 '15 at 21:06
    
added var_dump output – edoras May 30 '15 at 21:10
up vote 1 down vote accepted

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. :)

share|improve this answer
    
Really your code could use a fair bit of restructuring... leads to a number of problems. I realize what I posted above isn't going to cut the bill in your bigger picture. – Markus AO May 30 '15 at 21:31
    
success!!! Thank you so much for your kind insight! – edoras May 30 '15 at 21:32
    
if you are open to sharing, what other problems do you forsee? – edoras May 30 '15 at 21:33
    
I do get the full list of arrays as expected after modifying as you shared. This includes removing the 2nd if{}. This gives me an array that later on in the function I can insert into the db. What was the bigger picture you were thinking of? – edoras May 30 '15 at 21:42
    
Well if it works, it works. But suppose you want to add more fields in the future, etc. you will have to add more of those individual conditions. I updated my post above with an alternative way to do this. Lands you with a neat array of any fields with characters followed by digits. You could basically scrap most of the initial code too. You'd of course have to append the user id to each sub-array. Less maintenance this way. :) – Markus AO May 30 '15 at 21:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.