日期数组
array(
0 => array("date_1" => "06-09-2010"),
1 => array("date_2" => "07-09-2010"),
2 => array("date_3" => "08-09-2010")
)
日数组
array(
0 => array("day_1" => "Monday"),
1 => array("day_2" => "Tuesday"),
2 => array("day_3" => "Wednesday")
)
期间数组
array(
0 => array("period_1" => "1"),
1 => array("period_2" => "1"),
2 => array("period_3" => "1")
)
如何更改/合并数组以使数组成为这样的?
array 0 = date_1,day_1, period_1
array 1 = date_2,day_2, period_2
array 2 = date_3,day_3, period_3
使用数组合并将返回类似这样的内容
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")
)
已编辑(答案)
使用此功能可以解决问题。谢谢 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);
答案 0 :(得分:2)
您可以尝试:
$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);
答案 1 :(得分:0)
如果您的3个数组相同长度并使用相同的键编制索引,则可以运行此命令:
$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]);
}
编辑:如果您的密钥不是数字,则可以使用array_merge_recursive
来获得所需内容。如果您想依赖基本语言函数,这可能是一个更干净的选项。
http://www.php.net/manual/en/function.array-merge-recursive.php