我有一个数组,其中包含作为用户注释的字符串
阵列可以是这样的:
//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
或任何其他包含空字符串和注释的表单
我需要的是一个字符串和firs不是空的评论。
在示例1中,字符串应包含:"this is one comment">
在示例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
如果您的PHP版本不能取消引用。。。
foreach($array as $key => $value)
{
if(trim($value)) return $value;
}
return "No comments found";