How can I strip whitespace only elements from an array?

前端 未结 5 1366
有刺的猬
有刺的猬 2021-01-23 05:54

How I can remove all elements of an array that contain just whitespace, not whitespace in an element like \"foobar \" but just empty array elements like <

5条回答
  •  长发绾君心
    2021-01-23 06:11

    This code takes advantage of the callback parameter for array_filter. It will loop the array, call trim() on the value, and remove it if the resulting value evaluates to false. (which an empty string will)

    $a = array_filter($a, 'trim');

提交回复
热议问题