Remove all elements in array matching a specific string using PHP

后端 未结 2 2023
情深已故
情深已故 2021-01-04 10:00

I\'m hoping this is really simple, and I\'m missing something obvious!

I\'m trying to remove all elements in an array that match a certain string. It\'s a basic 1D a

2条回答
  •  误落风尘
    2021-01-04 10:20

    You can just use array_diff here, if it's one fixed string:

    $array = array_diff($array, array("Remove"));
    

    For more complex matching, I'd use preg_grep obviously:

    $array = preg_grep("/^Remove$/i", $array, PREG_GREP_INVERT);
    // matches upper and lowercase for example
    

提交回复
热议问题