我有一个像这样的数组
["2018-10-01","2018-10-15","Night Shift","20:00:00","06:00:00"],
["2018-10-16","2018-10-31","regular work schedule","09:00:00","18:00:00"],
["2018-11-01","2018-11-08","Night Shift","20:00:00","06:00:00"]
我想将两个日期放在同一数组中。
[["2018-10-01","2018-10-15"],"Night Shift","20:00:00","06:00:00"],
[["2018-10-16","2018-10-31"],"regular work schedule","09:00:00","18:00:00"],
[["2018-11-01","2018-11-08"],"Night Shift","20:00:00","06:00:00"]
我是php的新手,以及如何在php中做到这一点
答案 0 :(得分:0)
要在数组(多维数组)中包含数组,您要做的就是在声明中指定数组。
$test = array(array("2018-10-01","2018-10-15"),"Night Shift","20:00:00","06:00:00")
因此要访问变量,例如获取第一个日期或第二个日期,您可以回显:
echo $test[0][0]; //first date
echo $test[0][1]; //second date
echo $test[1]; //the words, "Night Shift"
答案 1 :(得分:0)
您可以迭代数组,并按以下方式操作前两个元素:
$arr = array( ["2018-10-01","2018-10-15","Night Shift","20:00:00","06:00:00"],["2018-10-16","2018-10-31","regular work schedule","09:00:00","18:00:00"],["2018-11-01","2018-11-08","Night Shift","20:00:00","06:00:00"]);
foreach($arr as &$elem) {
$a = array_shift($elem); // get the first date to var a and remove from elem
$elem[0] = array($a, $elem[0]); // insert to index 0 array with both dates
}
echo print_r($arr);
这将输出:
Array (
[0] => Array
(
[0] => Array
(
[0] => 2018-10-01
[1] => 2018-10-15
)
[1] => Night Shift
[2] => 20:00:00
[3] => 06:00:00
)
[1] => Array
(
....