我有一个字符串,如下所示
Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]我想将这个字符串分解为多维数组,并将Mon、Tue、Wed这样的日子作为键存储,将方括号中的值作为以下每一天的值存储,并以较小的数组形式访问每一天。
Array
(
[Mon] => [3,9]
[Tue] => [3,9]
[Wed] => [5,9]
[Thu] => [5,11]
[Fri] => [5,11]
[Sat] => [5,11]
[Sun] => [4,10]
)使用下面的代码,我能够实现它,但是像[3,9] or [5,11]这样的值被视为字符串。
$atd = $utd = "";
$dod = "20-12-2020";
$daysArray = "Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]";
$days = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
preg_match_all("/\[[^\]]*\]/", $daysArray, $matches);
$dayNum = $matches[0];
$daysArray = array_combine($days , $dayNum);
print_r($daysArray);
$valArray = array();
foreach($daysArray as $day=>$val){
if($day == "Mon" || $day == "Tue"){
$atd = date("d-m-Y", strtotime("+".$val[0]." days", $dod));
$utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
}else if($day == "Sun"){
$atd = date("d-m-Y", strtotime("+".$val[0]." days", $dod));
$utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
}else if($day == "Wed"){
$atd = date("d-m-Y", strtotime("+".$val[0]." days", $dod));
$utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
}else{
$atd = date("d-m-Y", strtotime("+".$val[0]." 5 days", $dod));
$utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
}
}
} 当我打印$valArray时,它是一个像下面这样的数组,这是非常糟糕的。
array(
[0]=>[
[1]=>4
[2]=>,
[3]=>1
[4]=>0
[5]=>]
);请帮帮忙
发布于 2021-01-04 15:16:04
我想这就是你想要的结果。
$str = 'Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]';
$result = [];
$bits = explode(' ', $str); // explode on space between day names
foreach ( $bits as $bit) {
$b = explode('[', $bit); // $bit = Mon[3,9]
$b[1] = rtrim($b[1], ']'); // $b0 = Mon, B1 = 3,9] so trim off the last ]
$result[$b[0]] = explode(',', $b[1]); // explode on , to get inner array
}
print_r($result);结果
Array
(
[Mon] => Array
(
[0] => 3
[1] => 9
)
[Tue] => Array
(
[0] => 3
[1] => 9
)
[Wed] => Array
(
[0] => 5
[1] => 9
)
[Thu] => Array
(
[0] => 5
[1] => 11
)
[Fri] => Array
(
[0] => 5
[1] => 11
)
[Sat] => Array
(
[0] => 5
[1] => 11
)
[Sun] => Array
(
[0] => 4
[1] => 10
)
)发布于 2021-01-04 15:12:54
试试这个:
$str = 'Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]';
// convert main string to array by explode by empty space
$arr = explode(' ', $str);
$newArr = [];
foreach ($arr as $data) {
// to get all b/n brackets best is to use regEx
preg_match('#\[(.*?)\]#', $data, $match);
// for key of new array use all befor 1st bracket
// and for content explode data b/n brackets by ','
$newArr[strtok($data, '[')] = explode(',', $match[1]);
}
var_dump($newArr);结果:
array(7) { ["Mon"]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "9" }
["Tue"]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "9" }
["Wed"]=> array(2) { [0]=> string(1) "5" [1]=> string(1) "9" }
["Thu"]=> array(2) { [0]=> string(1) "5" [1]=> string(2) "11" }
["Fri"]=> array(2) { [0]=> string(1) "5" [1]=> string(2) "11" }
["Sat"]=> array(2) { [0]=> string(1) "5" [1]=> string(2) "11" }
["Sun"]=> array(2) { [0]=> string(1) "4" [1]=> string(2) "10" }
}发布于 2021-01-04 15:22:06
以下是一个建议:
$days = "Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]";
$individualDays = explode(' ', $days);
$result = [];
foreach ($individualDays as $individualDay) {
$dates = preg_match_all('/\d+/', $individualDay, $matches);
$result[substr($individualDay, 0, 3)] = $matches[0];
}它的工作原理:
Mon[3,9]、Tue[3,9]等)。substr)。preg_match_all中的所有数字,并将匹配数组分配给该键。https://stackoverflow.com/questions/65564886
复制相似问题