Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

Best way to loop PHP and grab variables for insert to mysql

My var_dump on the array that gets passed is:
array(6) {
  ["_token"]=>
  string(40) "zQ2tv4UljUsdwd7MeA6aKQW1PMQBBEOhB1EDFrc5"
  ["airline"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "4"
    [2]=>
    string(1) "2"
  }
  ["flightNumber"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "4"
    [2]=>
    string(1) "6"
  }
  ["departing"]=>
  array(3) {
    [0]=>
    string(18) "07/24/2017 1:58 PM"
    [1]=>
    string(18) "07/29/2017 1:58 PM"
    [2]=>
    string(18) "07/26/2017 1:59 PM"
  }
  ["seatNumber"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "4"
    [2]=>
    string(2) "55"
  }
  ["tripID"]=>
  string(2) "22"
}

Open in new window


How would I loop these and still keep their association for inserting.  

For example all of the arrays with the "0" key are associated, same with "1", "2".
SOLUTION
Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nathan Riley

ASKER

Got it thanks, I think since some fields are optional this method may not work.  I'll need to update my form and add another array to accommodate for this issue.

So instead of airline[], flightNumber[] ect....

identifier[airline[]
identifier[flightnumber[]
I  think I need a better understanding of your data layout.  Can you give me an example of a data set that won't have an equal number of entries?  That should be easy enough to overcome, but I would need to know what you mean.
You can see the form here, I've updated the names now so I can do a single foreach on the storage to database part.

http://groupplanner.gallitin.com/flight/create?depart=07%2F20%2F2017&return=08%2F05%2F2017&tripID=22&tripName=ABC#
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, yeah that makes sense, will update.  Only issue I see is that I need to modify the names slightly as I continue to clone/add more fields:

Page loads it looks like this:
flight0[]airline[]

Open in new window


As I clone I need to increase to:
flight1[]airline[]

Open in new window


Not sure how to do that though, opened another question on it in case you want to comment: https://www.experts-exchange.com/questions/29046111/jQuery-update-array-name-on-clone.html
No, you don't need to add the numbers at the end, like you are doing.  It is implicitly doing that in the form for every one created.  Much like it did for the other fields in your original example.  Try it, and you will see what I mean.
Ok so just

flight[]airline[]

Right?  I'll give a try and post var_dump.
Hmm...well that gives me an output like:

array(3) {
  ["_token"]=>
  string(40) "r7tvg2FQcsvBY8vlS5PA4ByDWfWQkm6GZTvg92Bk"
  ["flight"]=>
  array(8) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(18) "07/24/2017 8:53 PM"
    [3]=>
    string(1) "2"
    [4]=>
    string(1) "3"
    [5]=>
    string(1) "1"
    [6]=>
    string(18) "07/29/2017 8:53 PM"
    [7]=>
    string(1) "1"
  }
  ["tripID"]=>
  string(2) "22"
}

Open in new window


They are all in one big mess without any names or separation.

But when I increment flight0[]['airline'], flight1[]['airline'], flight2[]['airline'] manually in the inspector, then submit I get:
array(4) {
  ["_token"]=>
  string(40) "r7tvg2FQcsvBY8vlS5PA4ByDWfWQkm6GZTvg92Bk"
  ["flight0"]=>
  array(4) {
    [0]=>
    array(1) {
      ["'airline'"]=>
      string(1) "1"
    }
    [1]=>
    array(1) {
      ["'flightNumber'"]=>
      string(1) "1"
    }
    [2]=>
    array(1) {
      ["'departing'"]=>
      string(18) "07/24/2017 8:58 PM"
    }
    [3]=>
    array(1) {
      ["'seatNumber'"]=>
      string(1) "1"
    }
  }
  ["flight1"]=>
  array(4) {
    [0]=>
    array(1) {
      ["'airline'"]=>
      string(1) "3"
    }
    [1]=>
    array(1) {
      ["'flightNumber'"]=>
      string(1) "2"
    }
    [2]=>
    array(1) {
      ["'departing'"]=>
      string(18) "07/29/2017 8:58 PM"
    }
    [3]=>
    array(1) {
      ["'seatNumber'"]=>
      string(1) "2"
    }
  }
  ["tripID"]=>
  string(2) "22"
}

Open in new window


To me the second option is a lot cleaner as they are separated.
Looks like I wasn't thinking that through correctly.  This leads me back to my original solution.  I'm not sure why you feel it won't work.  Try the original solution with some values and output the result.  It shouldn't matter if some variables are null...if it is to be expected.  Just check for that in the input data.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Used Jim's comments to help construct my final needed solution.