从php数组中删除特定字符

时间:2014-12-17 10:52:24

标签: php arrays filter

我有这个数组我想删除

array (

        [79] => 0.0
        [80] => 0.0
        [81] => 0.0
        [82] => 0.0
        [83] => 0.0
        [84] => 0.0
        [85] => 0.0
        [86] => 0.0
        [87] => 0.0
        [88] => 0.0
        [89] => 0.0
        [90] => 2.0
        [91] => 13.0
        [92] => 17.0
        [93] => 0.0
        [94] => 0.0
        [95] => 0.0
        [96] => 0.0
        [97] => 0.0
        [98] => 0.0
        [99] => 0.0
        [100] => 3.0

    )

我想删除0.0怎么能轻易做到请帮帮我 我想用循环

来做这个

4 个答案:

答案 0 :(得分:3)

您可以使用' array_filter'从PHP函数过滤null,空字符串或0.0值

$array = array_filter($array); 

答案 1 :(得分:0)

我会选择上面的Benz回答

但另一种选择可能是array_diff

$remove = array(0.0);
$filtered = array_diff($source_array, $remove);

答案 2 :(得分:0)

function is_zero($string) {
    $remove = array("0", "0.0");
    if (in_array($string, $remove)) {
        return FALSE;
    }
    return TRUE;
}

$clean_array = array_filter($your_array, "is_zero");

这将删除0或0.0的所有内容。使用强制转换比较(int / float)可能比检查字符串更好......

在array_filter()

上查看http://php.net/manual/en/function.array-filter.php有关信息的信息

答案 3 :(得分:0)

您可以尝试以下操作:

<?php

$array=array('0.0','0.0','0.0','0.0','0.0','0.0','0.0','0.0','2.0','13.0','17.0','3.0');

function myFilter($string) {
  return strpos($string, '0.0') === false;
}

$newArray = array_filter($array, 'myFilter');

echo '<pre>';
print_r($newArray);
echo '<pre>';

?>