我想在php中进行以下转换:
from: "0110001" to: "Tuesdays, Wednesdays, Sundays".
这样做的奖励积分:
from: "0110001" to: "Tuesdays, Wednesdays and Sundays".
输入是七个字符,每个字符代表一周中的一天。
如果没有 foreach 循环,我怎么能做到这一点?我想使用 array_walk 或 array_reduce。
工作解决方案,但有一个 foreach :
<?php
function parseDays($str) {
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
$result = array();
foreach (str_split($str) as $k=>$v) {
if ($v == '1') {
$result[] = $days[$k];
}
}
return join(', ', $result);
}
echo parseDays("0110001");