1

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 !

0

7 Answers 7

4

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>"; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Edited again, first version now; Second version won't work with $test=0 (integer)
1

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>"; } } 

Comments

1

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!

7 Comments

@RiaD excuse me? This is actually an excellent answer, and more maintainable than many of the others. In fact, this explicitly does a test for "not empty string" which is what the OP wanted, rather than simply testing for 0 length. A minor quibble, yes, and they both accomplish the same thing, but it's a point regardless.
Not sure what you mean. Would you kindly remove your -1, or explain how this post is harmfully incorrect? I accept downvotes for being fundamentally wrong about something, but I take offense to this one.
When I post it, it was foreach( $test_arr as $key => $value ) { //empty } (or maybe my eyes is strange %) Can't remove -1 unless you edit
Hmm, very strange. I'll try to think of some more info to add. Thanks!
removed, sorry for this situation;)
|
1

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); 

Comments

0

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.

Comments

0

just a simple typing error:

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

Comments

0

Try this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.