我在mysql db
的单个字段中有以下值$rec1="4/1 @ 4p, 4/7 @ 4p, 4/8 @ 4p, 4/14 @ 4p, 4/15 @ 4p, 4/22 @ 4p, 4/28 @ 4p ";
$rec2="4/1 @ 4p, 4/7 @ 7p, 4/8 @ 4p, 4/14 @ 4p, 4/15 @ 4p, 4/22 @ 4p, 4/28 @ 4p ";
我需要确定所有时间值是否相同,如果是,则返回单个时间值,否则返回整个字符串。
在上面的两个记录中,我希望返回值为 -
$rec1=4p
(所有时间都相同,不关心日期)
来自原始$rec2= unchanged
的{{1}},因为至少有一个时间值不同。
答案 0 :(得分:1)
试一试:
$rec1="4/1 @ 4p, 4/7 @ 4p, 4/8 @ 4p, 4/14 @ 4p, 4/15 @ 4p, 4/22 @ 4p, 4/28 @ 4p ";
$rec2="4/1 @ 4p, 4/7 @ 7p, 4/8 @ 4p, 4/14 @ 4p, 4/15 @ 4p, 4/22 @ 4p, 4/28 @ 4p ";
function wtf($over) {
$split = split(",",$over);
$times = array();
foreach ($split as $schedule) {
$parts = split("@",$schedule);
$times[] = trim($parts[1]);
}
$times = array_unique($times);
if (count($times) == 1) return $times[0];
return $over;
}
echo wtf($rec1);
echo "<BR>";
echo wtf($rec2);
答案 1 :(得分:0)
使用array_unique
或array_count_values