开发者

PHP Array foreach question

I have a question about arrays and foreach.

If i have an array like this:

$test_arr = array();
$test_arr['name1'] = "an example sentence";
$test_arr['anything'] = "dsfasfasgsdfg";
$test_arr['code'] = "4334refwewe";
$test_arr['empty1'] = "";
$test_arr['3242'] = 开发者_运维知识库"";

how can I do a foreach and "pick" only the ones that have values? (in my array example, would only take the first 3 ones, name1, anything and code).

I tried with

foreach ($test_arr as $test) {
  if (strlen($test >= 1)) {
    echo $test . "<br>";
  }
}

but it doesn't work. Without the "if" condition it works, but empty array values are taken into consideration and I don't want that (because I need to do a <br> after each value and I don't want a <br> if there is no value)

Sorry if I don't explain myself very well, I hope you understand my point. Shouldn't be too difficult I guess..

Thanks for your help !


Maybe will work

foreach ($test_arr as $test) {
  if (strlen($test)!=="") {
    echo $test . "<br>";
  }
}

Your solution with corrected syntax:

foreach ($test_arr as $test) {
  if (strlen($test)>=1) {
    echo $test . "<br>";
  }
}


Since empty strings are false, you could just do this (but you'd exclude 0's with the if):

foreach ($test_arr as $key => $val) {
  if ($val) {
    echo $val. "<br>";
  }
}

If it has to be an empty string then (excluding 0 and FALSE):

foreach ($test_arr as $key => $val) {
  // the extra = means that this will only return true for strings.
  if ($val !== '' ) {
    echo $val. "<br>";
  }
}


Since it looks like you're using an associative array, you should be able to do this:

foreach( $test_arr as $key => $value )
{
    if( $value != "" )
    {
        echo $value . "<br />";
    }
}

As shown, you can test $value for an empty string directly. Since this is precisely the test you are trying to accomplish, I would hope that this would solve your problem perfectly.

On another note, this is pretty straight forward and should be very maintainable in the future when you've forgotten exactly what it was that you were doing!


You are better off to use a while loop like this:

while(list($test_key, $test_value) = each($test_arr))
{
    if($test_value != "") { echo $test_value . "<br/>"; }
}
reset($test_arr);

If your array gets large, the while will be much faster. Even on small arrays, I have noticed a big difference in the execution time.

And if you really don't want the array key. You can just do this:

while(list(, $test_value) = each($test_arr))
{
    if($test_value != "") { echo $test_value . "<br/>"; }
}
reset($test_arr);


You can check if the value is emtpy with empty().

Note that values like 0 or false are considered empty as well, so you might have to check for string length instead.


just a simple typing error:

foreach ($test_arr as $test) {
  if (strlen($test) >= 1) {
    echo $test . "<br>";
  }
}


Try this:

     foreach ($test_arr as $test) {
          if (strlen($test) > 0) {
             echo $test . "<br>";
          } 
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜