array of array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    #1

    array of array

    I've to save in an array of array some values.

    Problem: I don't know the size of the array before filling it.

    I'd like something like

    $bigarray = array():

    then runnig the code I must create dinamically (I don't know how many) few
    arrays in the bigarray;
    $bigarray["X"] = array():
    $bigarray["Y"] = array():

    then fill those arrays with few unknow number of values.
    array_push($big array["X"],$value1);
    array_push($big array["X"],$value2);

    array_push($big array["Y"],$value5);
    ....
    but array_push doesn't manage it.

    How to do so ?

    Bob


  • Kim André Akerø

    #2
    Re: array of array

    Bob Bedford wrote:
    [color=blue]
    > I've to save in an array of array some values.
    >
    > Problem: I don't know the size of the array before filling it.
    >
    > I'd like something like
    >
    > $bigarray = array():
    >
    > then runnig the code I must create dinamically (I don't know how
    > many) few arrays in the bigarray; $bigarray["X"] = array():
    > $bigarray["Y"] = array():
    >
    > then fill those arrays with few unknow number of values.
    > array_push($big array["X"],$value1);
    > array_push($big array["X"],$value2);
    >
    > array_push($big array["Y"],$value5);
    > ...
    > but array_push doesn't manage it.
    >
    > How to do so ?[/color]

    $bigarray["X"][] = $value1;
    $bigarray["X"][] = $value2;
    ....
    $bigarray["Y"][] = $value5;

    --
    Kim André Akerø
    - kimandre@NOSPAM betadome.com
    (remove NOSPAM to contact me directly)

    Comment

    • Ewoud Dronkert

      #3
      Re: array of array

      Bob Bedford wrote:[color=blue]
      > array_push($big array["Y"],$value5);
      > but array_push doesn't manage it.[/color]

      Try $big_array['Y'][] = $value5;
      In the manual it says "has the same effect" but maybe the implementation
      is different? I don't know. It also says:

      "Note: If you use array_push() to add one element to the array it's better
      to use $array[] = because in that way there is no overhead of calling a
      function."

      --
      E. Dronkert

      Comment

      • Bob Bedford

        #4
        Re: array of array

        > $bigarray["X"][] = $value1;[color=blue]
        > $bigarray["X"][] = $value2;
        > ...
        > $bigarray["Y"][] = $value5;[/color]
        Does it create a new element in the $bigarray["X"] ?
        I mean then I have
        $bigarray["X"][0] == $value1;
        $bigarray["X"][1] == $value2;

        Bob

        Comment

        • Kim André Akerø

          #5
          Re: array of array

          Bob Bedford wrote:
          [color=blue][color=green]
          > > $bigarray["X"][] = $value1;
          > > $bigarray["X"][] = $value2;
          > > ...
          > > $bigarray["Y"][] = $value5;[/color]
          > Does it create a new element in the $bigarray["X"] ?
          > I mean then I have $bigarray["X"][0] == $value1;
          > $bigarray["X"][1] == $value2;[/color]

          Yes. As mentioned in Ewoud Dronkert's reply, this is a better way of
          doing it rather than using array_push(). Even the PHP manual mentions
          this.

          $bigarray[] = $value1;
          =>
          $bigarray[0] == $value1;

          $bigarray[0][] = $value2;
          =>
          $bigarray[0][0] == $value2;

          --
          Kim André Akerø
          - kimandre@NOSPAM betadome.com
          (remove NOSPAM to contact me directly)

          Comment

          Working...