Variable Array Name in foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • farrell.niall@gmail.com

    Variable Array Name in foreach

    Hi,
    I'm trying to use nested foreach loops to loop through two arrays.
    Each element in the first array corresponds to another array. The
    brandcode part of the first array is the same as the name of the
    corresponding array.
    Basically, one array is of the brand info, and the second is of the
    cars made by that brand.
    To loop through all the brands, and then print their corresponding
    cars, I tried:
    foreach ($brands as $curbrand) {
    foreach (${$curbrand['brandcode']} as $curcar) {
    echo $curcar;
    }
    }
    But I get "Invalid argument supplied for foreach()"
    Any ideas?

  • Clay Colburn

    #2
    Re: Variable Array Name in foreach

    This should work for you...

    foreach ($brands as $curbrand) {
    foreach ($curbrand as $brandcode =$curcar) {
    echo $curcar;
    }
    }

    the syntax above in the inner loop is for when you have key value
    pairs in the array of interest.

    so that line could be rewritten, foreach($curbra nd as $key =$value)

    Hopefully this is what you are looking for.



    On Mar 22, 12:17 pm, farrell.ni...@g mail.com wrote:
    Hi,
    I'm trying to use nested foreach loops to loop through two arrays.
    Each element in the first array corresponds to another array. The
    brandcode part of the first array is the same as the name of the
    corresponding array.
    Basically, one array is of the brand info, and the second is of the
    cars made by that brand.
    To loop through all the brands, and then print their corresponding
    cars, I tried:
    foreach ($brands as $curbrand) {
    foreach (${$curbrand['brandcode']} as $curcar) {
    echo $curcar;
    }}
    >
    But I get "Invalid argument supplied for foreach()"
    Any ideas?

    Comment

    • Shimin

      #3
      Re: Variable Array Name in foreach

      Clay Colburn wrote:
      This should work for you...
      >
      foreach ($brands as $curbrand) {
      foreach ($curbrand as $brandcode =$curcar) {
      echo $curcar;
      }
      }
      >
      the syntax above in the inner loop is for when you have key value
      pairs in the array of interest.
      >
      so that line could be rewritten, foreach($curbra nd as $key =$value)
      >
      Hopefully this is what you are looking for.
      >
      >
      >
      On Mar 22, 12:17 pm, farrell.ni...@g mail.com wrote:
      >Hi,
      >I'm trying to use nested foreach loops to loop through two arrays.
      >Each element in the first array corresponds to another array. The
      >brandcode part of the first array is the same as the name of the
      >correspondin g array.
      >Basically, one array is of the brand info, and the second is of the
      >cars made by that brand.
      >To loop through all the brands, and then print their corresponding
      >cars, I tried:
      >foreach ($brands as $curbrand) {
      > foreach (${$curbrand['brandcode']} as $curcar) {
      > echo $curcar;
      > }}
      >>
      >But I get "Invalid argument supplied for foreach()"
      >Any ideas?
      >
      >
      It's clearly not what the OP is looking for.

      Comment

      • niallfarrell

        #4
        Re: Variable Array Name in foreach

        On Mar 22, 9:23 pm, Shimin <smguo2...@gmai l.comwrote:
        Clay Colburn wrote:
        This should work for you...
        >
        foreach ($brands as $curbrand) {
        foreach ($curbrand as $brandcode =$curcar) {
        echo $curcar;
        }
        }
        >
        the syntax above in the inner loop is for when you have key value
        pairs in the array of interest.
        >
        so that line could be rewritten, foreach($curbra nd as $key =$value)
        >
        Hopefully this is what you are looking for.
        >
        On Mar 22, 12:17 pm, farrell.ni...@g mail.com wrote:
        Hi,
        I'm trying to use nested foreach loops to loop through two arrays.
        Each element in the first array corresponds to another array. The
        brandcode part of the first array is the same as the name of the
        corresponding array.
        Basically, one array is of the brand info, and the second is of the
        cars made by that brand.
        To loop through all the brands, and then print their corresponding
        cars, I tried:
        foreach ($brands as $curbrand) {
        foreach (${$curbrand['brandcode']} as $curcar) {
        echo $curcar;
        }}
        >
        But I get "Invalid argument supplied for foreach()"
        Any ideas?
        >
        It's clearly not what the OP is looking for.
        No, that didn't do it but thanks anyway. I figured it out eventually -
        this seems to do the trick:
        foreach ($brands as $curbrand) {
        $cars = ${$curbrand[brandcode]};
        echo $curbrand[brandname];
        foreach ($cars as $curcar) {
        echo $curcar;
        }
        }
        It basically echoes the brandname followed by all the cars in the
        array of the same name. $cars = ${$curbrand[brandcode]} was the main
        bit I was looking for - assigning a variable as the array name.

        Comment

        • peter

          #5
          Re: Variable Array Name in foreach

          I'm trying to use nested foreach loops to loop through two arrays.
          Each element in the first array corresponds to another array. The
          brandcode part of the first array is the same as the name of the
          corresponding array.
          Basically, one array is of the brand info, and the second is of the
          cars made by that brand.
          To loop through all the brands, and then print their corresponding
          cars, I tried:
          foreach ($brands as $curbrand) {
          foreach (${$curbrand['brandcode']} as $curcar) {
          echo $curcar;
          }
          }
          But I get "Invalid argument supplied for foreach()"
          Any ideas?
          I am not 100% sure I have understood your problem but the following works
          for the way I envisaged your arrays:-

          $brands = array('porsche' , 'ford');
          $porsche = array('911');
          $ford = array('escort') ;
          foreach ($brands as $r)
          {
          foreach (${$r} as $model)
          {
          echo $model;
          }
          }

          The error you have suggests that you are not passing an array to the foreach
          loop instead you are passing a value.


          Comment

          • OmegaJunior

            #6
            Re: Variable Array Name in foreach

            On Thu, 22 Mar 2007 22:23:56 +0100, Shimin <smguo2001@gmai l.comwrote:
            It's clearly not what the OP is looking for.
            I love it when people state this, and fail to provide an explanation, let
            alone state what, according to them, is a correct answer to what the OP is
            looking for.

            --
            Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

            Comment

            Working...