简体   繁体   中英

How to remove an empty element in array php

Hi i have a string which has <br/> in it, which is converted to an array.

$files = "Pretty Hurts<br/>
          Ghost/Haunted<br/>
          Drunk in Love (feat. Jay-Z)<br/>
          Blow<br/>
          No Angel<br/>
          Yoncé/Partition<br/>
          Jealous<br/>
          Rocket<br/>
          Mine (feat. Drake)
          <br/>"
 $files_to_array = explode('<br/>', $files);

when i print_r the array there's an empty element in the array i tried trimming the string before i add it to the array, empty elemnt still appears. how can i solve this?

You can use array_filter($files_to_array, "trim") . array_filter without the second parameter returns all values from the original array that aren't considered equal to false . An empty string is equal to false , but a string of whitespace is not. trim overcomes this.
PHP array_filter manual page

we have array_filter function for that

simply do this

array_filter($files_to_array);

and to rebuilt the sequential keys use array_values

so it'll be array_values( array_filter($files_to_array));

$files = "Pretty Hurts<br/>
      Ghost/Haunted<br/>
      Drunk in Love (feat. Jay-Z)<br/>
      Blow<br/>
      No Angel<br/>
      Yoncé/Partition<br/>
      Jealous<br/>
      Rocket<br/>
      Mine (feat. Drake)
      <br/>";
$files_to_array = explode('<br/>', trim($files, '<br/>'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM