将字符串转换为多维数组以在php中计算日期



我有一个字符串,如下

Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]

我想将此字符串分解为多维数组,并将周一、周二、周三等日子存储为关键字,方括号内的值存储为每天的值,如下所示,并将每天作为较小的数组访问

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]=>]
);

请帮助

我想这就是你想要的结果。

$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
)
)

试试这个:

$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" } 
}

这里有一个建议:

$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中的所有数字,并将匹配数组分配给该键

您还可以使用捕获组和preg_replace_callback使用模式来匹配各个部分。

通过引用回调传递一个数组,将组1的值设置为键,并传递一个值为组2和组3的数组。

([a-z]+)[(d+),(d+)]

解释

  • ([a-z]+)捕获组1,匹配1+a字符a-z
  • [匹配[
  • (d+),(d+)在捕获组2和组3中捕获1+个数字,并在两者之间匹配一个,
  • ]比赛结束]

查看regex演示和Php演示

使用不区分大小写的匹配/i的示例

$pattern = '/([a-z]+)[(d+),(d+)]/i';
$str = 'Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]';
$result = [];
preg_replace_callback(
$pattern,
function ($m) use (&$result) {
$result[$m[1]] = [$m[2], $m[3]];
},
$str
);
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
)
)

此解决方案通过替换字符将字符串转换为JSON字符串。要创建所需的数组,只需要一个json_decode((。

$input = 'Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]';
$strJson = '{"'.strtr($input,[" " => ',"','[' => '":['])."}";
$array = json_decode($strJson, true);
//test output
echo "<pre>".var_export($array, true)."/<pre>";

相关内容

  • 没有找到相关文章

最新更新