0

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);
3
  • 3
    Surely some line breaks there won't kill you... Commented Sep 6, 2010 at 2:10
  • use var_export next time instead of var_dump Commented Sep 6, 2010 at 2:17
  • Thanks @Yi Jiang for the edit. @Yanic Okay i will try use var_export next time. Commented Sep 6, 2010 at 2:30

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.