[英]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. 嗨,我有一个包含
<br/>
的字符串,该字符串被转换为数组。
$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. 当我print_r数组时,数组中有一个空元素,我尝试在将字符串添加到数组之前修剪字符串,但空元素仍然出现。 how can i solve this?
我该如何解决?
You can use array_filter($files_to_array, "trim")
. 您可以使用
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
. 没有第二个参数的
array_filter
返回原始数组中所有不等于false
。 An empty string is equal to false
, but a string of whitespace is not. 空字符串等于
false
,但空白字符串不是。 trim
overcomes this. trim
克服了这一点。
PHP array_filter manual page PHP array_filter手册页
we have array_filter
function for that 我们有
array_filter
函数
simply do this 只需这样做
array_filter($files_to_array);
and to rebuilt the sequential keys use array_values
并使用
array_values
重建顺序键
so it'll be array_values( array_filter($files_to_array));
所以它将是
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/>'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.