vincej Posted March 23, 2012 Share Posted March 23, 2012 Hi - I am passing $_POST data from an HTML form. The POST array looks like this: Array ( [location] => Array ( [0] => Collingwood [1] => Varsity ) [4] => 01 Apr 2012 [1] => 12 Apr 2012 [2] => 01 May 2012 [3] => 01 Jun 2014 [8] => 01 Jul 2012 [6] => 12 Aug 2012 [7] => 20 Apr 2013 [5] => 20 Apr 2014 [submit] => Save Changes ) I am using a foreach loop to separate the keys from the values. The $values include dates and locations. I then pass $values to strtotime to convert the dates to a Unix Time stamp. when strtotime sees the Locations - it throws a PHP error "strtotime() expects parameter 1 to be string, array given" so my question is what can I do to separate the Dates from the locations in order to avoid the error ? MANY THANKS to anyone who can give advice on this ! Quote Link to comment https://forums.phpfreaks.com/topic/259574-how-should-i-separate-date-strings-from-sub-arrays-within-the-main-array/ Share on other sites More sharing options...
codebyren Posted March 23, 2012 Share Posted March 23, 2012 Since your date fields don't seem to have pre-defined keys in the HTML (and consequently the $_POST array), try something like this: <?php // Prepare an array to stash values successfully converted to timestamps $timestamps = array(); // Loop through post values and try convert each non-array value to a timestamp foreach ($_POST as $key => $value) { if ( ! is_array($value)) { $time = strtotime($value); # returns FALSE on failure if ($time) $timestamps[] = $time; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259574-how-should-i-separate-date-strings-from-sub-arrays-within-the-main-array/#findComment-1330563 Share on other sites More sharing options...
DavidAM Posted March 23, 2012 Share Posted March 23, 2012 1) Test to see if the value is an array and don't process it foreach ($_POST as $key => $value) { if (is_array($value)) continue; // ... } Of course, then strtotime will choke on the submit button. I would recommend changing the field names in your HTML: <INPUT name="dates[$ID]" ...> # Where $ID is whatever ID or Key you are using now # or, if that's not an ID and just a sequential array index <INPUT name="dates[]" ...> # use empty brackets Then process only the dates foreach($_POST['dates'] as $id => $value) { // Here we get only the dates } [edit] Guess I type too slow [/edit] Quote Link to comment https://forums.phpfreaks.com/topic/259574-how-should-i-separate-date-strings-from-sub-arrays-within-the-main-array/#findComment-1330566 Share on other sites More sharing options...
vincej Posted March 26, 2012 Author Share Posted March 26, 2012 HI Guys ! Thank you for y our feedback ! Just to clarify, my challenge is to process BOTH the locations and the dates, not to filter them out as such. Of course, as per my earlier post, when my code processes the dates strfttime() chokes on the locations. I am a bit of a newb, so perhaps this infor might help your suggestions a bit. Here is an excerpt of the relevant code for the HTML form and the Data Update Model ( btw - it is produced with Codeigniter, but the code is obvious ) HTML Form: <?php foreach ($pickupdates as $item) { $theDates = explode(',', $item['Dates']); $theIDs = explode(',', $item['ID']); ?> <tr align="center"> <td width="100" align="center"><?php echo $item['locationid'];?></td> <td width="100" align="center"><?php $data = array('name'=>'location[]', 'size'=>20,'value' => $item['location']); echo form_input($data); ?></td> <td width="100"><?php $data = array('name'=> $theIDs[0],'size'=>15,'value' =>(strftime("%d %b %Y",$theDates[0]))); echo form_input($data);?></td> <td width="100"><?php $data = array('name'=> $theIDs[1],'size'=>15,'value' =>(strftime("%d %b %Y",$theDates[1]))); echo form_input($data);?></td> <td width="100"><?php $data = array('name'=> $theIDs[2],'size'=>15,'value' => (strftime("%d %b %Y",$theDates[2]))); echo form_input($data); ?></td> <td width="100"><?php $data = array('name'=> $theIDs[3],'size'=>15,'value' => (strftime("%d %b %Y",$theDates[3]))); echo form_input($data);?></td> <td width="150"> <?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?></td> </tr> <?php ;} ?> </table> <?php form_input($data); echo "<div class=submitted>". form_submit('submit','Save Changes')."</div>"; echo form_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259574-how-should-i-separate-date-strings-from-sub-arrays-within-the-main-array/#findComment-1331241 Share on other sites More sharing options...
vincej Posted March 26, 2012 Author Share Posted March 26, 2012 Thank you very much for your help I have it working - in the end I changed the filed names in the HTML Cheers ! Quote Link to comment https://forums.phpfreaks.com/topic/259574-how-should-i-separate-date-strings-from-sub-arrays-within-the-main-array/#findComment-1331322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.