查找字符串的一部分并将其从数组中删除

时间:2013-10-14 13:34:16

标签: php arrays

我需要找到哪个是包含值的一部分的键而不显示它

$arr = array(0 => "helbor_000", 1 => "ez_000", 2 => "xx_333");
    $find = "helb";
    $key = array_search($find, $arr);
    foreach($arr as $index => $value){
          if($index != $key){
              echo $value;
          }
    }

3 个答案:

答案 0 :(得分:2)

preg_grep()可以做到

$arr = array(0 => "helbor_000", 1 => "ez_000", 2 => "xx_333");
$find = "~^helb.*$~";
$ret = preg_grep($find, $arr, PREG_GREP_INVERT);
print_r($ret);

答案 1 :(得分:1)

$arr = array(0 => "helbor_000", 1 => "ez_000", 2 => "xx_333");
$find = "helb";
foreach($arr as $index => $value){
    if(strpos($value, $find) === false) {
        echo $value, PHP_EOL;
    }
}

答案 2 :(得分:0)

echo $arr;
       ^

应为$ key或$ value;