Jump to content

[SOLVED] help with adding data to every member of the array please


tomasd

Recommended Posts

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!

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
)


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);
?>

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?

<?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);

?>

<?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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.