Multi-Dimensional Arrays In PHP ... ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David T. Ashley

    Multi-Dimensional Arrays In PHP ... ???

    Hi,

    Aside from the obvious recursive syntax for initialization,

    $x = array( ... array( ...)),

    what is the best way to deal with multi-dimensional arrays in PHP?

    It seems there is no syntax like $x[i][j][k]???

    Thanks, Dave.



  • Rik

    #2
    Re: Multi-Dimensional Arrays In PHP ... ???

    David T. Ashley wrote:
    Hi,
    >
    Aside from the obvious recursive syntax for initialization,
    >
    $x = array( ... array( ...)),
    array_fill() is a good one for some uses, range() fo others.
    what is the best way to deal with multi-dimensional arrays in PHP?
    Euhm, treat them as arrays? What do you want to do with them
    It seems there is no syntax like $x[i][j][k]???
    There is, almost exactly like it. Remember to quote the kaynames though.

    Grtz,
    --
    Rik Wasmus


    Comment

    • Chung Leong

      #3
      Re: Multi-Dimensional Arrays In PHP ... ???


      David T. Ashley wrote:
      Hi,
      >
      Aside from the obvious recursive syntax for initialization,
      >
      $x = array( ... array( ...)),
      >
      what is the best way to deal with multi-dimensional arrays in PHP?
      >
      It seems there is no syntax like $x[i][j][k]???
      >
      Thanks, Dave.
      What exactly are you trying to do?

      The following is perfectly legal syntax:

      <?php

      $x[0][1][17] = "Something" ;
      $x[0][34][11] = "Something else";

      ?>

      Comment

      • Erwin Moller

        #4
        Re: Multi-Dimensional Arrays In PHP ... ???

        David T. Ashley wrote:
        Hi,
        >
        Hi,
        Aside from the obvious recursive syntax for initialization,
        >
        $x = array( ... array( ...)),
        >
        what is the best way to deal with multi-dimensional arrays in PHP?
        That question is too general to give a sensible answer.
        It depends on what you need to accomplish.
        eg: You can use the hash indexing, or the normal (integer) indexing.
        Why should 1 be better? Depends on your situation.
        If you are using a database an associative indexing could make sense for
        rows. In other situations the integer indexing makes more sense.
        >
        It seems there is no syntax like $x[i][j][k]???
        Yes there is.
        >
        Thanks, Dave.
        Dave, read this:


        It will clear up a lot of questions.
        And one tip: In my experience: Instead of thinking of multidimensiona l
        arrays, it is often simpler and clearer to think of arrays that contain
        arrays.

        Regards,
        Erwin Moller

        Comment

        • Sandman

          #5
          Re: Multi-Dimensional Arrays In PHP ... ???

          In article <Pnexg.61535$gN 5.51649@fe10.us enetserver.com> ,
          "David T. Ashley" <dta@e3ft.comwr ote:
          Hi,
          >
          Aside from the obvious recursive syntax for initialization,
          >
          $x = array( ... array( ...)),
          >
          what is the best way to deal with multi-dimensional arrays in PHP?
          >
          It seems there is no syntax like $x[i][j][k]???
          Of course there is.


          <?
          $monkey["foo"]["bar"]["oof"]["rab"] = "foobar";
          print_r($monkey );
          ?>

          Output:
          Array
          (
          [foo] =Array
          (
          [bar] =Array
          (
          [oof] =Array
          (
          [rab] =foobar
          )

          )

          )

          )



          --
          Sandman[.net]

          Comment

          • David T. Ashley

            #6
            Re: Multi-Dimensional Arrays In PHP ... ???

            "Erwin Moller"
            <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c omwrote in
            message news:44c5d84f$0 $31653$e4fe514c @news.xs4all.nl ...
            David T. Ashley wrote:
            >>
            >It seems there is no syntax like $x[i][j][k]???
            >
            Yes there is.
            OK, all questions answered, thanks. In the online documentation, it seemed
            to suggest that only way to generate a multi-dimensional array was something
            like:

            $sub1 = array("apples", "oranges", "bananas", 2*82, 3.1415);
            $sub2 = array(24, 57, 91);

            $array[0] = $sub1;
            $array[1] = $sub2;

            This will work, but is awkward.

            So, if then

            echo $array[0][1];

            will give "oranges", then I'm happy.

            The syntax is there in the language. Thanks.



            Comment

            Working...