tags:

views:

92

answers:

2

Hi i have 3 array

Date Array

array(
   0 => array("date_1" => "06-09-2010"),
   1 => array("date_2" => "07-09-2010"),
   2 => array("date_3" => "08-09-2010")
)

Day Array

array(
   0 => array("day_1" => "Monday"),
   1 => array("day_2" => "Tuesday"),
   2 => array("day_3" => "Wednesday")

)

Period Array

array(
   0 =>  array("period_1" => "1"),
   1 =>  array("period_2" => "1"),
   2 =>  array("period_3" => "1")
) 

How can i alter/merge the array so the array will become something like this?

array 0 = date_1,day_1, period_1
array 1 = date_2,day_2, period_2
array 2 = date_3,day_3, period_3

Using array merge will return something like this

array(
   0 => array("date_1" => "06-09-2010"),
   1 => array("date_2" => "07-09-2010"),
   2 => array("date_3" => "08-09-2010"),
   3 => array("period_1" => "1"),
   4 => array("period_2" => "1"), 
   5 => array("period_3" => "1"),  
   6 => array("day_1" => "Monday"),
   7 => array("day_2" => "Tuesday"),
   8 => array("day_3" => "Wednesday")
)

Edited (Answer)

The problem can be solve using this function. Thanks Codaddict!

$date = array(
array('date_1'=>"06-09-2010"),
array('date_2'=>"07-09-2010"));

$day = array(
    array("day_1"=>"Monday"),
    array("day_2"=>"Tuesday")
);

$period = array(
    array("period_1"=>1),
    array("period_2"=>2)
);

$result = array(); 
for($i=0;$i<count($date);$i++) {
    array_push($result,array($data[$i],$day[$i],$period[$i]));
}
var_dump($result);
+2  A: 

You can try:

$data = array(
    array('date_1'=>"06-09-2010"),
    array('date_2'=>"07-09-2010")
);
$day = array(
    array("day_1"=>"Monday"),
    array("day_2"=>"Tuesday")
);
$period = array(
    array("period_1"=>1),
    array("period_2"=>2)
);

$result = array(); 

// assuming all arrays are of same size.
for($i=0;$i<count($data);$i++) {
    array_push($result,array($data[$i],$day[$i],$period[$i]));
}
var_dump($result);
codaddict
hi thanks this is what i looking for. thanks again codadict :)
cyberfly
A: 

Provided your 3 arrays are the same length and are indexed with the same keys, you could probably run this:

$merged = array();
//iterate on the keys
foreach(array_keys($date_array) as $index)
  //merge the sub arrays
  $merged[$index] = array_merge(array(), $date_array[$index], $day_array[$index], $period_array[$index]);
}

Edit: If your keys were not numeric, you would be able to use array_merge_recursive to get what you want. This might be a cleaner option if you want to rely on a base language function. http://www.php.net/manual/en/function.array-merge-recursive.php

Guillaume Bodi
thanks for the answer.
cyberfly