How can I strip whitespace only elements from an array?

前端 未结 5 1329
有刺的猬
有刺的猬 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:20

    $array = array('foo','  ','bar ');
    
    foreach ($array as $key => $value) {
        if (trim($value) == '') unset($array[$key]);
    }
    

    Array when dumped then contains:

    array(2) {
      [0]=>
      string(3) "foo"
      [2]=>
      string(4) "bar "
    }
    

提交回复
热议问题