Foreach with an array reference (objects)


I am making use of a reference to an array of objects in a foreach loop. I think I am missing something. I keep getting the following warning:

Invalid argument supplied for foreach()

I am aware that warnings can be ignored, but I thought if I can eliminate them, its better for my code.

As an example, consider:

//getObjectArray() retrieves an array of objects
$objArr = &getObjectArray();

foreach($objArr as $obj)//warning generated here
   echo "Name:" . $obj->name;
   echo "Surnam:". $obj->surname;

Any help on how I can eliminate the warning will be most appreciated!


Returns by reference is a bad practice. Don’t do it unless you have a very specific reason for it. You also need to check if your function is even returning an array - it could be returning false.

//getObjectArray() retrieves an array of objects
$objArr = getObjectArray();

if (is_array($objArr)) {
  foreach($objArr as $obj) {
    echo "Name:" . $obj->name;
    echo "Surnam:". $obj->surname;
} else {
    echo 'no results';

Many thanks for the reply, Morris.

I have special reason to do so. Developing an MVC component for Joomla 1.5. Due to the implementation of their API, references are necessary to prevent server overheads. Particularly with large objects, performance can be affected.

Any help on how to eliminate the warning will be much appreciated

Let us also assume that data is returned.



This will show you what’s in the $objArr variable, and what kind of thingy it is.

If it’s not an array, or an object which implements the array functions, foreach won’t be able to loop through it (as Michael already pointed out).

Thanks for your reply, Immerse.

Am fully aware of that though. Let us assume that we have expected data returned i.e. an array of objects, each with the fields name and surname.

If the function does not return something iterable, it does give that error. Assuming it returns an array without actually checking doesn’t change that…

It does return an array which I can iterate through but that warning is still output. I stated the assumption so we dont focus on what is returned but on the specific warning and if it can be eliminated

Objects in PHP are references by default. You do not need to implicitly use the pass by reference operator, nor should you. Also, with non-objects PHP performs copy on edit at an engine level, so the memory impact isn’t there.

More on references.
PHP: References Explained - Manual

Note Bene:

And, to be blunt, if you are having difficulty with a foreach iteration I sincerely doubt you have a technical reason for it.

In the code I gave you a way to test to see if you where getting what you expect - an array of objects. You need to test for that before iterating or the warning will pop up. Joomla doesn’t exactly have the most consistent API out there (neither does core PHP for that matter), so I’m guessing the function is returning boolean “false” to alert you that there are no objects found. You can’t iterate over “false” so foreach emits a warning.

So check and see what the function is actually returning.

I find print_r($myArray) easier to read than var_dump(), to get it to format the tabs/indentaion use:

echo ‘<pre>’ . print_r($myArray, true) . ‘</pre>’;

Use the xdebug module in PHP to get very pretty (and readable, useful) results from var_dump:

Managed to eliminate the warning.

Many thanks for your replies and tips. Though my question was focused more on the warning, I do appreciate that everyone is trying to help :slight_smile: . I also agree on var_dump, xdebug, print_r etc…invaluable debugging tools!

What was the cause of the problem?

Not too sure, Salathe. Out of frustration,I re-wrote that section of the code and revised the associated functions. I am convinced it is the same, but evidently it was not…because now it works with no warnings