tomasd Posted July 25, 2007 Share Posted July 25, 2007 Hi, I've got an array: Array ( [0] => 06:30 [1] => 08:10 [2] => 09:35 [3] => 12:00 [4] => 15:45 [5] => 17:10 [6] => 19:35 ) I need to add a date (Y-m-d) before the time. I have tried using array_walk, and this is something I came up with: <?php array_walk($original_array, 'add_date'); /* add date function */ function add_date($item) { $date_stack = array(); $iso_date = date('Y-m-d'); array_push($date_stack, "$iso_date $item:00"); print_r ($date_stack); ?> I'm getting following output Array ( [0] => 2007-08-04 06:30:00 ) Array ( [0] => 2007-08-04 08:10:00 ) Array ( [0] => 2007-08-04 09:35:00 ) Array ( [0] => 2007-08-04 12:00:00 ) Array ( [0] => 2007-08-04 15:45:00 ) Array ( [0] => 2007-08-04 17:10:00 ) Array ( [0] => 2007-08-04 19:35:00 ) Can somebody advise me how do I combine all these arrays to one array? Thanks very much! Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 I don't know if this is the best way to do this or if it's even what you want, but here ya go. <?php $date_time = Array("06:30","08:10","09:35","12:00","15:45","17:10","19:35"); function somename($array) { for ($i = 0; $i < sizeof($array); $i++) { $str = date("Y-m-d") . " " . $array[$i]; $array[$i] = $str; } return $array; } print "Output before function call: \n"; print_r($date_time); $date_time = somename($date_time); print "Output after function call: \n"; print_r($date_time); ?> output : Output before function call: Array ( [0] => 06:30 [1] => 08:10 [2] => 09:35 [3] => 12:00 [4] => 15:45 [5] => 17:10 [6] => 19:35 ) Output after function call: Array ( [0] => 2007-07-25 06:30 [1] => 2007-07-25 08:10 [2] => 2007-07-25 09:35 [3] => 2007-07-25 12:00 [4] => 2007-07-25 15:45 [5] => 2007-07-25 17:10 [6] => 2007-07-25 19:35 ) Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 25, 2007 Share Posted July 25, 2007 alternatively, if you want to stick to the way you were doing it originally (which will overwrite the original array): <?php $original = $original_array; array_walk($original_array, 'add_date'); /* add date function */ function add_date($item) { $iso_date = date('Y-m-d'); return "$iso_date $item:00"; } print_r($original_array); ?> Quote Link to comment Share on other sites More sharing options...
tomasd Posted July 25, 2007 Author Share Posted July 25, 2007 perfect! This is exactly what I was after, thanks to both of you! I've used the 2nd solution as it looks "cleaner" cheers Quote Link to comment Share on other sites More sharing options...
tomasd Posted July 25, 2007 Author Share Posted July 25, 2007 actually I've realized that it didn't give the date... with: <?php $original_array = Array("06:30","08:10","09:35","12:00","15:45","17:10","19:35"); array_walk($original_array, 'add_date'); /* add date function */ function add_date($item) { $iso_date = date('Y-m-d'); return "$iso_date $item:00"; } print_r ($original_array); ?> I get: Array ( [0] => 06:30 [1] => 08:10 [2] => 09:35 [3] => 12:00 [4] => 15:45 [5] => 17:10 [6] => 19:35 ) Can you see where the problem is? Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 <?php $original_array = Array("06:30","08:10","09:35","12:00","15:45","17:10","19:35"); array_walk($original_array, 'add_date'); /* add date function */ function add_date(&$item) { $iso_date = date('Y-m-d'); $item = "${iso_date} ${item}:00"; } print_r ($original_array); ?> or <?php $original_array = Array("06:30","08:10","09:35","12:00","15:45","17:10","19:35"); array_walk($original_array, create_function('&$item',' $item = date(\'Y-m-d\') . " " . $item; ')); print_r ($original_array); ?> Quote Link to comment Share on other sites More sharing options...
tomasd Posted July 25, 2007 Author Share Posted July 25, 2007 <?php $original_array = Array("06:30","08:10","09:35","12:00","15:45","17:10","19:35"); array_walk($original_array, 'add_date'); /* add date function */ function add_date(&$item) { $iso_date = date('Y-m-d'); $item = "${iso_date} ${item}:00"; } print_r ($original_array); ?> or <?php $original_array = Array("06:30","08:10","09:35","12:00","15:45","17:10","19:35"); array_walk($original_array, create_function('&$item',' $item = date(\'Y-m-d\') . " " . $item; ')); print_r ($original_array); ?> Thanks very much it worked just fine! Thank you. Quote Link to comment 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.