开发者

PHP delete value from array if var is found in the value

I have an array that looks like this:

A开发者_Go百科rray
(
   [0] => aaaaa
   [1] => bbbbb
   [2] => ccxcc
   [3] => ddddd
)

I want do delete every value of the array that contains the letter x, so this would be the outcome:

Array
(
   [0] => aaaaa
   [1] => bbbbb
   [2] => ddddd
)

How would I do this?

Thanks!


You can use array_filter()

$output = array_filter($input, function ($v) { return strpos($v, 'x') === FALSE; });

If you are using PHP < 5.3.0:

function filter_x($v) { 
    return strpos($v, 'x') === FALSE; 
}

$output = array_filter($input, 'filter_x');


foreach ($array as $k => $v) { // Loop the array
  if (strpos($v,'x') !== FALSE) { // Check if $v has a letter x in it
    unset($array[$k]); // Delete the element
  }
}
array_merge($array); // Put the remaining keys in a contiguous order


for($i = 0; $i < count($array); $i++) {
    if(strpos($array[$i], 'x') !== false) {
        unset($array[$i]);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜