How to remove all PHP array elements containing a certain sub-string?

心已入冬 提交于 2019-12-01 21:22:35

You can use array_filter and stripos

$array = array(1 => 'January', 'February', 'March');
print_r(array_filter($array, function ($var) { return (stripos($var, 'Jan') === false); }));
MonkeyMonkey

You can use array_filter() with a closure (inline-function):

array_filter(
  $array,
  function ($element) use ($to_remove) {
    return strpos($element, $to_remove) === false;
  }
);

(PHP Version >= 5.3)

The simplest way is with array_filter. This function receives the array to filter and a callback function that does the actual filtering based on the value received:

function filter_func( $v )
{
  return ( ( $var % 2 ) == 0 );
}
$test_array = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
print_r( array_filter( $test_array, "filter_func" ) );

Hope helped!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!