我试图将我的数组分解为:
[0] => Array
(
[0] => 14 // this is hours
[1] => 38 // this is minutes
[2] => 14 // this is hours again
[3] => 59 // this is minutes again
)
[1] => Array
(
[0] => 15 // this is hours
[1] => 10 // this is minutes
[2] => 16 // this is hours again
[3] => 40 // this is minutes again
)
.
.
.
[200] => Array
(
[0] => 13 // this is hours
[1] => 35 // this is minutes
[2] => 23 // this is hours again
[3] => 32 // this is minutes again
)
这是因为我将来会比较这些时间。
我有这样的时间列表:
15:48,16:10 12:01,12:19 13:06,13:28 10:45,11:02
现在我把它放在一个看起来像这样的数组中:
[0] => 16:10,16:36
[1] => 13:06,13:17
.
.
.
[200] => 14:38,14:59
到目前为止我尝试了什么
$length = count($timesArr);
for($i=0; $i < $length; $i++){
foreach (explode(',', $timesArr[$i]) as $piece) {
$timesArray[] = explode(':', $piece);
}
}
,这非常接近,因为结果输出如下:
[0] => Array
(
[0] => 14
[1] => 38
)
[1] => Array
(
[0] => 14
[1] => 59
)
所以主要的问题是我需要如上所示,细胞0和1在同一个细胞中
我也可以直接从字符串中删除它,这样我就会爆炸字符串。
答案 0 :(得分:2)
答案 1 :(得分:1)
preg_split('/[,:]/', $string)
应将时间字符串拆分为:和,
答案 2 :(得分:0)
preg_split()
应该足够了
这有效 -
$timesArr = Array(
"16:10,16:36",
"13:06,13:17",
"14:38,14:59"
);
foreach($timesArr as $time){
$res[] = preg_split("/[\,\:]/",$time);
}
var_dump($res);
/*
array
0 =>
array
0 => string '16' (length=2)
1 => string '10' (length=2)
2 => string '16' (length=2)
3 => string '36' (length=2)
1 =>
array
0 => string '13' (length=2)
1 => string '06' (length=2)
2 => string '13' (length=2)
3 => string '17' (length=2)
2 =>
array
0 => string '14' (length=2)
1 => string '38' (length=2)
2 => string '14' (length=2)
3 => string '59' (length=2)
*/
答案 3 :(得分:0)
这很容易,但preg_split()
很酷:
$string = '15:48,16:10 12:01,12:19 13:06,13:28 10:45,11:02';
$array = array_chunk(explode(':', str_replace(array(',',' '), ':', $string)), 4);
print_r($array);
答案 4 :(得分:0)
这是另一种解决方案
$times = array( "16:10,16:36", '13:06,13:17');
$startendtimes = array();
foreach($times as $time){
list($start,$end) = explode(',',$time);
list($starthour,$startmin) = explode(':',$start);
list($endhour,$endmin) = explode(':',$end);
$startendtimes[] = array( $starthour,$startmin,$endhour,$endmin );
}
var_dump($startendtimes);
/* Outputs
array (size=2)
0 =>
array (size=4)
0 => string '16' (length=2)
1 => string '10' (length=2)
2 => string '16' (length=2)
3 => string '36' (length=2)
1 =>
array (size=4)
0 => string '13' (length=2)
1 => string '06' (length=2)
2 => string '13' (length=2)
3 => string '17' (length=2)
*/