SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach with an array reference (objects)

    Hi,

    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:

    Code:
    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:
    PHP Code:

    //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!

    tino
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tinonetic View Post
    Hi,

    I am making use of a reference to an array of objects in a foreach loop.

    PHP Code:

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

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

    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.

    Code php:
    //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';
    }

  3. #3
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Try

    PHP Code:
    var_dump($objArr); 
    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).

  5. #5
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    Try

    PHP Code:
    var_dump($objArr); 
    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.
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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...
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    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
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tinonetic View Post
    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.
    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:
    Quote Originally Posted by PHP Manual
    Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so. (PHP: Returning References - Manual)
    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.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>';

  10. #10
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Use the xdebug module in PHP to get very pretty (and readable, useful) results from var_dump:


  11. #11
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 . I also agree on var_dump, xdebug, print_r etc...invaluable debugging tools!
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming

  12. #12
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    What was the cause of the problem?
    Salathe
    Software Developer and PHP Manual Author.

  13. #13
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •