我有一个包含用户评论字符串的数组
数组可以像这样:
//example 1
$comments
: array =
0: string =
1: string =
2: string = this is one comment
或像这样:
//example 2
$comments
: array =
0: string = hey, I am a comment
1: string =
2: string =
3: string = and this is another comment
或包含空字符串和注释的任何其他形式
我需要的是一个带有非空注释的字符串。
在示例1中,字符串应包含:“这是一个注释”
在示例2中,“嘿,我是评论”
我怎样才能做到这一点?我会绕着这个转转,它必须简单得多。
万分感谢!
这是使用forloop的方法:
<?php
$ex1 = array("", "", "comment");
$ex2 = array("comment", "", "");
function getFirstNotEmpty($arr) {
for ($i = 0; $i < count($arr); $i++)
if (!empty($arr[$i]))
return $arr[$i];
}
echo getFirstNotEmpty($ex1) . "\n";
echo getFirstNotEmpty($ex2);
输出:
comment
comment
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句