Forum Moderators: coopster

Message Too Old, No Replies

foreach with empty array

         

mooger35

4:01 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



In searching for a solution to having a warning thrown when using foreach with an empty array (aside from having to use if count > 0) I came across this:

foreach((array) $array as $key => $val){
//loop
}

Is there any drawback to using this method? And will it remove the warnings?

rocknbil

4:53 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what you mean, if the array is empty it should just do nothing(?) Are you sure you don't mean the array itself is undefined?

Calling undefined elements is an easy fix. Just define them before you start.

$thisthing = new Array();
$thatthing = null; // scalar

If you foreach through $thisthing, or concatenate to $thatthing, it shouldn't give you any errors.

mooger35

5:01 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



A warning will occur though:

Warning: Invalid argument supplied for foreach() in ...

rocknbil

6:19 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well it was an error, but only for the "new" keyword, fault of working in too many languages.

Show me how this errors, I get no errors or warnings. Maybe you're talking about something else, if it's an array of arrays, same deal, define them first.

<?php
header("content-type:text/html");

echo "<p>Define arrays and variables.</p>";

$thisthing = Array();
$thatthing = null; // scalar

foreach ($thisthing as $key => $value) { echo "k $key v $value <br>"; }

echo $thatthing;

?>

dreamcatcher

6:23 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use empty [php.net] to check the array isn`t empty before using a foreach.

$array = array();
if (!empty($array)) {
foreach ($array as..) {
}
}

dc

mooger35

6:36 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



thanks dreamcatcher but that's basically what I was trying to avoid... having to use the extra { } like

if(count($array) > 0){
foreach ($array as..) {
}
}

or as you put it using empty.

I believe using foreach((array) $array as... removes the need for those (can't test where I am unfortunately). I just found it in php.net and was curious if it did do what I want (avoid the use of an extra check).

// By casting the $non_array to an (array) type, it will function without error, skipping the loop

foreach ((array) $non_array as $key => $val) {
print "Key $key, Value $val\n";
}


Obvioulsy being a warning I can remove that using ini_set but I'd prefer to have it working without the warnings.

coopster

11:14 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The issue isn't with foreach, it's with the type of variable you are iterating, or attempting to iterate I should say. The PHP foreach [php.net] control structure will never throw a warning on an empty array. It will throw a warning because the argument you are offering is not an array. That is why you don't receive the warning when you cast it as an array. rocknbil is showing you how to overcome that by first initializing the variable before populating it with data and then iterating over it. For example, lets say you expect the array to come from checkboxes that were posted. PHP by default will create an array for you if you set them up correctly in your form by using the brackets. However, what if the array of values is coming from somewhere else where it may or may not be an array? It can happen, and if you code long enough you will find that it will indeed happen. What to do? Cast it!
$myExpectedArray = (array) $arrayFromSomewhere; 
foreach ($myExpectedArray as $key => $value) {
// process the array
}

You are doing the same thing, just inside the construct. You can do it either way without issue.

I think what the members here are saying is that if you are uncertain as to the origin of the variable, meaning you don't know if it is an array or not, you should probably be doing some other form of editing and casting of the variable prior to iterating over it with a control structure and processing.

mooger35

4:51 am on Jul 22, 2010 (gmt 0)

10+ Year Member



Thanks coopster, that actually explains it perfectly for me. After testing it further when I was able to I realize my issue was my mysql class function wasn't giving me an array when returning no results on the database query.

Thanks guys

mooger35

6:03 am on Jul 22, 2010 (gmt 0)

10+ Year Member



as an edit... what I actually need is:

foreach((object)$array as $key => $val){...

that seems to do what I need. Sorry for the confusion.