foreach and multidimensional arrays

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

    foreach and multidimensional arrays

    Hi.

    Does for each for on multidimensiona l arrays? For example, if one
    element of $in is $course_list and I want to echo all everything in
    course_list

  • Andy Hassall

    #2
    Re: foreach and multidimensiona l arrays

    On 27 Jan 2005 15:16:31 -0800, ubccis@gmail.co m wrote:
    [color=blue]
    >Does for each for on multidimensiona l arrays? For example, if one
    >element of $in is $course_list and I want to echo all everything in
    >course_list[/color]

    foreach operates on arrays, and only to one level; if the array contains
    arrays, foreach doesn't care - the loop index will just be set to an array that
    is that dimension of the array.

    You could use is_array() if you want to tell if you've hit another dimension
    of the array.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Chris Hope

      #3
      Re: foreach and multidimensiona l arrays

      ubccis@gmail.co m wrote:
      [color=blue]
      > Does for each for on multidimensiona l arrays? For example, if one
      > element of $in is $course_list and I want to echo all everything in
      > course_list[/color]

      No. You'd need to test the values in the array to see if they were also
      arrays and do something with that eg

      foreach($foo as $index => $value) {
      if(is_array($va lue)) {
      ... do something ...
      }
      }

      --
      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      • ubccis@gmail.com

        #4
        Re: foreach and multidimensiona l arrays

        Hi....

        I found the answer...of using nested arrays...


        foreach ($in as $key) {
        if(is_array($ke y)){
        foreach ($key as $key2 => $value) {

        echo "Key: $key; Value: $value<br />\n";

        }
        } else {

        echo "Key: $key; Value: $value<br />\n";

        }

        }

        Comment

        • Geoff Berrow

          #5
          Re: foreach and multidimensiona l arrays

          I noticed that Message-ID:
          <1106871825.931 010.232570@c13g 2000cwb.googleg roups.com> from
          ubccis@gmail.co m contained the following:
          [color=blue]
          >I found the answer...of using nested arrays...[/color]
          What about n dimensional arrays?

          I'm thinking perhaps a recursive function?
          --
          Geoff Berrow (put thecat out to email)
          It's only Usenet, no one dies.
          My opinions, not the committee's, mine.
          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

          Comment

          • skrebbel

            #6
            Re: foreach and multidimensiona l arrays

            "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
            news:ovpjv0tj2s 0n71ebkvlj73jvh h2fpk3tpr@4ax.c om...[color=blue]
            > I noticed that Message-ID:
            > <1106871825.931 010.232570@c13g 2000cwb.googleg roups.com> from
            > ubccis@gmail.co m contained the following:
            >[color=green]
            > >I found the answer...of using nested arrays...[/color]
            > What about n dimensional arrays?
            >
            > I'm thinking perhaps a recursive function?[/color]

            yes.


            Comment

            • Geoff Berrow

              #7
              Re: foreach and multidimensiona l arrays

              I noticed that Message-ID: <ctdqbn$kgl$1@n ews.tue.nl> from skrebbel
              contained the following:
              [color=blue][color=green]
              >> I'm thinking perhaps a recursive function?[/color]
              >
              >yes.[/color]

              But I'd usually use print_r() if I just wanted to take a peek.

              --
              Geoff Berrow (put thecat out to email)
              It's only Usenet, no one dies.
              My opinions, not the committee's, mine.
              Simple RFDs http://www.ckdog.co.uk/rfdmaker/

              Comment

              Working...