我有这样的数组输出:-
Array
(
[0] => 25-08-2014
[1] => 26-08-2014
[2] => 27-08-2014
[3] => 28-08-2014
)
我需要把它转换成:-
$duration = array("25/08/2014", "26/08/2014", "27/08/2014");
我尝试在这个函数中使用它:-
if (in_array($dateOutput, $duration))
我该怎么做呢?
尽管(正如许多评论者所说),您应该展示您所尝试的。参见下面的解决方案:
$arr = array(
"25-08-2014",
"26-08-2014",
"27-08-2014"
);
function reformat($date_string)
{
return str_replace('/', '-', $date_string);
}
$arr_editted = array_map('reformat', $arr);
这将给出您想要的值。你也可以对array_map
使用匿名函数;但因为我不确定你的PHP版本,你应该使用这个。
您需要这样做:
$dateOutputs = array("25-08-2014", "26-08-2014", "27-08-2014", "28-08-2014");
$duration = array("25/08/2014", "26/08/2014", "27/08/2014");
foreach ($dateOutputs as $dateOutput) {
$neededDate = date('d/m/Y', strtotime($dateOutput));
if (in_array($neededDate, $duration)) {
// do something here
}
}
希望能有所帮助。
如果我理解正确,您想要的就是将-
更改为/
?
那么你可以这样写:
<?php
$dates = array("25-08-2014", "26-08-2014", "27-08-2014");
$total = count($dates);
for( $i=0; $i<$total; $i++) {
$dates[$i] = str_replace( '-', '/', $dates[$i] );
}
print_r( $dates );
?>
另一种选择是使用array_map()
函数:
<?php
function swapIt($value) {
return str_replace( '-', '/', $value );
}
$dates = array("25-08-2014", "26-08-2014", "27-08-2014");
$newDates = array_map( "swapIt", $dates );
print_r( $newDates );
?>