Nested foreach loop over same array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan S

    Nested foreach loop over same array

    I'm using next snippet:

    $somearray = array(...);

    foreach($somear ray as $item1) {
    foreach($item1 as $item2) {
    // ... do something ...
    }
    }

    ....and I'm getting next error:
    Invalid argument supplied for foreach()

    on second foreach.

    This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
    but site seems to work ok.


    Is there any way to avoid that error?


    Tnx.
  • Erwin Moller

    #2
    Re: Nested foreach loop over same array


    Ivan S schreef:
    I'm using next snippet:
    >
    $somearray = array(...);
    >
    foreach($somear ray as $item1) {
    foreach($item1 as $item2) {
    // ... do something ...
    }
    }
    >
    ...and I'm getting next error:
    Invalid argument supplied for foreach()
    >
    on second foreach.
    Hi Ivan,

    Nothing wrong with your code.
    The two foreach constructs should be no problem.
    I expect that the original array isn't containing what you expect it to
    contain.
    Since you use a foreach on every value in the $somearray, this array
    should contain of arrays itself.

    Test that like this:
    echo "<pre>";
    print_r($somear ray);
    echo "</pre>";
    exit;

    Regards,
    Erwin Moller



    >
    This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
    but site seems to work ok.
    >
    >
    Is there any way to avoid that error?
    >
    >
    Tnx.

    Comment

    • Olaf Schinkel

      #3
      Re: Nested foreach loop over same array

      Ivan S schrieb:
      I'm using next snippet:
      >
      $somearray = array(...);
      >
      foreach($somear ray as $item1) {
      foreach($item1 as $item2) {
      // ... do something ...
      }
      }
      >
      ...and I'm getting next error:
      Invalid argument supplied for foreach()
      >
      on second foreach.
      >
      This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
      but site seems to work ok.
      >
      >
      Is there any way to avoid that error?
      >
      >
      Tnx.
      The function is_array() exists :-)

      Comment

      • Ivan S

        #4
        Re: Nested foreach loop over same array

        On 9 srp, 12:06, Erwin Moller
        <Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
        Ivan S schreef:
        >
        I'm using next snippet:
        >
        $somearray = array(...);
        >
        foreach($somear ray as $item1) {
              foreach($item1 as $item2) {
                    // ... do something ...
             }
        }
        >
        ...and I'm getting next error:
        Invalid argument supplied for foreach()
        >
        on second foreach.
        >
        Hi Ivan,
        >
        Nothing wrong with your code.
        The two foreach constructs should be no problem.
        I expect that the original array isn't containing what you expect it to
        contain.
        Since you use a foreach on every value in the $somearray, this array
        should contain of arrays itself.
        >
        Test that like  this:
        echo "<pre>";
        print_r($somear ray);
        echo "</pre>";
        exit;
        >
        Regards,
        Erwin Moller
        >
        >
        >
        This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
        but site seems to work ok.
        >
        Is there any way to avoid that error?
        >
        Tnx.
        >
        >
        Hi Erwin. :)

        Your advice helped, some items in $somearray weren't arrays it self,
        that was reason why i got errors reported.

        Thanks for your help.

        Comment

        • Ivan S

          #5
          Re: Nested foreach loop over same array

          On 10 srp, 05:25, Olaf Schinkel <tr...@schinkel .tvwrote:
          Ivan S schrieb:
          >
          I'm using next snippet:
          >
          $somearray = array(...);
          >
          foreach($somear ray as $item1) {
                foreach($item1 as $item2) {
                      // ... do something ...
               }
          }
          >
          ...and I'm getting next error:
          Invalid argument supplied for foreach()
          >
          on second foreach.
          >
          This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
          but site seems to work ok.
          >
          Is there any way to avoid that error?
          >
          Tnx.
          >
          The function is_array() exists :-)
          I know that...I wasn't aware that my input array could contain
          unexpected values (such as non array values).

          Comment

          • Barry

            #6
            Re: Nested foreach loop over same array


            "Ivan S" <ivan.skugor@gm ail.comwrote in message
            news:f0ebdfc4-5660-4d1d-accb-b35f0946d9ff@i7 6g2000hsf.googl egroups.com...
            On 10 srp, 05:25, Olaf Schinkel <tr...@schinkel .tvwrote:
            Ivan S schrieb:
            >
            I'm using next snippet:
            >
            $somearray = array(...);
            >
            foreach($somear ray as $item1) {
            foreach($item1 as $item2) {
            // ... do something ...
            }
            }
            >
            ...and I'm getting next error:
            Invalid argument supplied for foreach()
            >
            on second foreach.
            >
            This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
            but site seems to work ok.
            >
            Is there any way to avoid that error?
            >
            Tnx.
            >
            The function is_array() exists :-)
            I know that...I wasn't aware that my input array could contain
            unexpected values (such as non array values).


            =========

            strictly speaking, avoid the error like this:

            $somearray = array(...);
            foreach ($somearray as $item1)
            {
            if (!is_array($ite m1)){ $item1 = array($item1); }
            foreach ($item1 as $item2)
            {
            // do something...
            }
            }

            that way, if $item1 is not an array but still a usable, otherwise valid,
            value then your next foreach will be able to process it normally...base d on
            whatever '...do something...' is.


            Comment

            Working...