如何找出数组有除php中指定字符串以外的变量?

时间:2015-07-31 12:15:49

标签: php arrays

我想在php中检查数组是否具有 0000-00-00 00:00:00 以外的值

Array ( [0] => 0000-00-00 00:00:00 
[1] => 2015-07-31 12:43:02 
[2] => 0000-00-00 00:00:00 
[3] => 0000-00-00 00:00:00 )

2015-07-31 12:43:02 时间可能会随时改变

4 个答案:

答案 0 :(得分:2)

你可以遍历数组并根据错误的日期测试每个值,如果它不坏,那就做你想做的任何事情。

$found_good = false;
foreach ( $arr as $i => $v ) {
    if ( $v != '0000-00-00 00:00:00' ) {
       $found_good  = true;
       echo "array[$i] has the valid date $v\n";
    }
}

答案 1 :(得分:2)

使用setCurrentItem(int item, boolean smoothScroll)

这是你的数组:

array_filter

以下是使用$items = [ 0 => '0000-00-00 00:00:00' 1 => '2015-07-31 12:43:02' 2 => '0000-00-00 00:00:00' 3 => '0000-00-00 00:00:00' ]; 计数来解决问题的方法:

array_filter

答案 2 :(得分:1)

您可以使用in_array

E.g:

$os = array("0000-00-00 00:00:00 ", "2015-07-31 12:43:02", "0000-00-00 00:00:00 ", "0000-00-00 00:00:00");
if (in_array("0000-00-00 00:00:00", $os)) {
    echo "Got Value";
}
else {
    echo "Got No value";
}

详细了解in_array click here

答案 3 :(得分:1)

不使用array_filter

的简单解决方案
$arr = array (0 => "0000-00-00 00:00:00", 
1=> "2015-07-31 12:43:02" ,
2 => "0000-00-00 00:00:00",
3 => "0000-00-00 00:00:00");
$ccount = count($arr);
$c=0;
foreach($arr as $a=>$b)
{
    if(in_array($b, $arr))
        $c++;
}
if($ccount == $c)
    echo "something present";
else
    echo "Something not present";