0

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!" 
}
10
  • 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? Commented May 30, 2015 at 20:59
  • I'm definitely seeing all 16 - as mentioned, I var_dump($_POST) before the foreach and they're all there Commented May 30, 2015 at 21:02
  • mind if you can post the 'var_dump(yourarray)' here? Commented May 30, 2015 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. Commented May 30, 2015 at 21:06
  • added var_dump output Commented May 30, 2015 at 21:10

1 Answer 1

1

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

6
  • 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. Commented May 30, 2015 at 21:31
  • if you are open to sharing, what other problems do you forsee? Commented May 30, 2015 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? Commented May 30, 2015 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. :) Commented May 30, 2015 at 21:46
  • thanks again @Markus AO! I do think I'm at the max number of fields, though I really appreciate the instruction towards cleaner, more efficient code, maximizing use built-in functions. it may take me time to try it out, won't be able to respond tonight, but will definitely do so by tomorrow. Really my sincere thanks to you! Commented May 30, 2015 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.