Adding arrays

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

    Adding arrays

    Consider the following multi-dimensional array:
    ---------------------------
    $arr = array(
    array(3, 5, 7, 9),
    array(2, 4, 6, 8),
    array(1, 3, 5, 7)
    );

    function add_arrays($arr ) {
    for ($row = 0; $row < count($arr[0]); $row++) {
    for ($column = 0; $column < count($arr[$column]); $column++) {
    $totals[$row] = $totals[$row] + $arr[$column][$row];
    }
    }
    print_r($totals );
    }

    add_arrays($arr );
    ---------------------------
    This returns the following array:
    Array ( [0] =6 [1] =12 [2] =18 [3] =24 )

    These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
    first elements), 12 (5 + 4 + 3, added all second elements), etc. This
    took me quite a portion of the day to construct ;) However, I would
    like add_arrays to return a multidimensiona l array like this:

    Array (
    Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
    Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
    row (i.e. 5 = 3 + 2)
    Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
    row (i.e. 6 = 3 + 2 + 1)
    )

    Could somebody please help me building a function that does just that?
    Adding one row to the previous one and adding the result to the output
    array? Any help would be greatly apprectiated!

  • Daz

    #2
    Re: Adding arrays

    On May 7, 4:05 pm, dennis.spreng.. .@gmail.com wrote:
    Consider the following multi-dimensional array:
    ---------------------------
    $arr = array(
    array(3, 5, 7, 9),
    array(2, 4, 6, 8),
    array(1, 3, 5, 7)
    );
    >
    function add_arrays($arr ) {
    for ($row = 0; $row < count($arr[0]); $row++) {
    for ($column = 0; $column < count($arr[$column]); $column++) {
    $totals[$row] = $totals[$row] + $arr[$column][$row];
    }
    }
    print_r($totals );
    >
    }
    >
    add_arrays($arr );
    ---------------------------
    This returns the following array:
    Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
    >
    These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
    first elements), 12 (5 + 4 + 3, added all second elements), etc. This
    took me quite a portion of the day to construct ;) However, I would
    like add_arrays to return a multidimensiona l array like this:
    >
    Array (
    Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
    Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
    row (i.e. 5 = 3 + 2)
    Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
    row (i.e. 6 = 3 + 2 + 1)
    )
    >
    Could somebody please help me building a function that does just that?
    Adding one row to the previous one and adding the result to the output
    array? Any help would be greatly apprectiated!
    Is there any guarantee that the arrays will all be the same length as
    one-another? Are they always a set length, or can the lengths vary?

    Personally, I would start with making an empty array, which is the
    same length as the size of the longest array. Set all values in that
    array to 0. Then, loop through each array, and loop through the values
    of each array, and add them to the starting array. Now return the
    array you just created. Hope that makes sense. Example to follow.

    Comment

    • Daz

      #3
      Re: Adding arrays

      On May 7, 4:05 pm, dennis.spreng.. .@gmail.com wrote:
      Consider the following multi-dimensional array:
      ---------------------------
      $arr = array(
      array(3, 5, 7, 9),
      array(2, 4, 6, 8),
      array(1, 3, 5, 7)
      );
      >
      function add_arrays($arr ) {
      for ($row = 0; $row < count($arr[0]); $row++) {
      for ($column = 0; $column < count($arr[$column]); $column++) {
      $totals[$row] = $totals[$row] + $arr[$column][$row];
      }
      }
      print_r($totals );
      >
      }
      >
      add_arrays($arr );
      ---------------------------
      This returns the following array:
      Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
      >
      These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
      first elements), 12 (5 + 4 + 3, added all second elements), etc. This
      took me quite a portion of the day to construct ;) However, I would
      like add_arrays to return a multidimensiona l array like this:
      >
      Array (
      Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
      Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
      row (i.e. 5 = 3 + 2)
      Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
      row (i.e. 6 = 3 + 2 + 1)
      )
      >
      Could somebody please help me building a function that does just that?
      Adding one row to the previous one and adding the result to the output
      array? Any help would be greatly apprectiated!
      I take that back, I misread your post. Sorry.

      Example to follow anyway. :D

      Comment

      • ZeldorBlat

        #4
        Re: Adding arrays

        On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
        Consider the following multi-dimensional array:
        ---------------------------
        $arr = array(
        array(3, 5, 7, 9),
        array(2, 4, 6, 8),
        array(1, 3, 5, 7)
        );
        >
        function add_arrays($arr ) {
        for ($row = 0; $row < count($arr[0]); $row++) {
        for ($column = 0; $column < count($arr[$column]); $column++) {
        $totals[$row] = $totals[$row] + $arr[$column][$row];
        }
        }
        print_r($totals );
        >
        }
        >
        add_arrays($arr );
        ---------------------------
        This returns the following array:
        Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
        >
        These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
        first elements), 12 (5 + 4 + 3, added all second elements), etc. This
        took me quite a portion of the day to construct ;) However, I would
        like add_arrays to return a multidimensiona l array like this:
        >
        Array (
        Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
        Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
        row (i.e. 5 = 3 + 2)
        Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
        row (i.e. 6 = 3 + 2 + 1)
        )
        >
        Could somebody please help me building a function that does just that?
        Adding one row to the previous one and adding the result to the output
        array? Any help would be greatly apprectiated!
        Here's one way. Obviously there are others, also.

        function add_arrays_cumu lative($arr) {
        $out = array();

        for($i = 0; $i < count($arr); $i++) {
        if(!isset($out[$i]))
        $out[$i] = array();

        for($j = 0; $j < count($arr[$i]); $j++)
        $out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
        $out[$i-1][$j] : 0);
        }

        return $out;
        }


        Comment

        • dennis.sprengers@gmail.com

          #5
          Re: Adding arrays

          The length of the array's may vary, as may the number of array's. But
          all array's will be of equal length, i.e.

          $arr = array(
          array(3, 5, 7, 9, 23, 53, 54, 57),
          array(2, 4, 6, 8, 23 , 2, 43, 5),
          array(1, 3, 5, 7, 56, 45, 8, 54),
          array(4, 4, 5, 3, 21, 3, 23, 42),
          array(6, 3, 2, 1, 8, 33, 12, 43)
          );

          is also possible but

          $arr = array(
          array(3, 5, 7, 9, 23, 53, 54, 57),
          array(2, 4, 6, 8, 23 , 2),
          );

          will never be :-) Hope this helps!


          Comment

          • dennis.sprengers@gmail.com

            #6
            Re: Adding arrays

            Wow, thanks! It works great. I will spend the next hour or so trying
            to understand what code you wrote and why I still can't come up with
            stuff solutions that :-)

            On 7 mei, 17:32, ZeldorBlat <zeldorb...@gma il.comwrote:
            On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
            >
            >
            >
            Consider the following multi-dimensional array:
            ---------------------------
            $arr = array(
            array(3, 5, 7, 9),
            array(2, 4, 6, 8),
            array(1, 3, 5, 7)
            );
            >
            function add_arrays($arr ) {
            for ($row = 0; $row < count($arr[0]); $row++) {
            for ($column = 0; $column < count($arr[$column]); $column++) {
            $totals[$row] = $totals[$row] + $arr[$column][$row];
            }
            }
            print_r($totals );
            >
            }
            >
            add_arrays($arr );
            ---------------------------
            This returns the following array:
            Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
            >
            These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
            first elements), 12 (5 + 4 + 3, added all second elements), etc. This
            took me quite a portion of the day to construct ;) However, I would
            like add_arrays to return a multidimensiona l array like this:
            >
            Array (
            Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
            Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
            row (i.e. 5 = 3 + 2)
            Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
            row (i.e. 6 = 3 + 2 + 1)
            )
            >
            Could somebody please help me building a function that does just that?
            Adding one row to the previous one and adding the result to the output
            array? Any help would be greatly apprectiated!
            >
            Here's one way. Obviously there are others, also.
            >
            function add_arrays_cumu lative($arr) {
            $out = array();
            >
            for($i = 0; $i < count($arr); $i++) {
            if(!isset($out[$i]))
            $out[$i] = array();
            >
            for($j = 0; $j < count($arr[$i]); $j++)
            $out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
            $out[$i-1][$j] : 0);
            }
            >
            return $out;
            >
            }

            Comment

            • Daz

              #7
              Re: Adding arrays

              On May 7, 4:40 pm, dennis.spreng.. .@gmail.com wrote:
              Wow, thanks! It works great. I will spend the next hour or so trying
              to understand what code you wrote and why I still can't come up with
              stuff solutions that :-)
              >
              On 7 mei, 17:32, ZeldorBlat <zeldorb...@gma il.comwrote:
              >
              On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
              >
              Consider the following multi-dimensional array:
              ---------------------------
              $arr = array(
              array(3, 5, 7, 9),
              array(2, 4, 6, 8),
              array(1, 3, 5, 7)
              );
              >
              function add_arrays($arr ) {
              for ($row = 0; $row < count($arr[0]); $row++) {
              for ($column = 0; $column < count($arr[$column]); $column++) {
              $totals[$row] = $totals[$row] + $arr[$column][$row];
              }
              }
              print_r($totals );
              >
              }
              >
              add_arrays($arr );
              ---------------------------
              This returns the following array:
              Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
              >
              These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
              first elements), 12 (5 + 4 + 3, added all second elements), etc. This
              took me quite a portion of the day to construct ;) However, I would
              like add_arrays to return a multidimensiona l array like this:
              >
              Array (
              Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
              Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
              row (i.e. 5 = 3 + 2)
              Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
              row (i.e. 6 = 3 + 2 + 1)
              )
              >
              Could somebody please help me building a function that does just that?
              Adding one row to the previous one and adding the result to the output
              array? Any help would be greatly apprectiated!
              >
              Here's one way. Obviously there are others, also.
              >
              function add_arrays_cumu lative($arr) {
              $out = array();
              >
              for($i = 0; $i < count($arr); $i++) {
              if(!isset($out[$i]))
              $out[$i] = array();
              >
              for($j = 0; $j < count($arr[$i]); $j++)
              $out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
              $out[$i-1][$j] : 0);
              }
              >
              return $out;
              >
              }
              Here's my version with comments. I posted it anyway, as it would have
              been a waste of time for me not to. I think it's identical to the
              example above, but it's a little longer...

              <?php

              $arr = array(
              array(3, 5, 7, 9),
              array(2, 4, 6, 8),
              array(1, 3, 5, 7)
              );

              function add_arrays($inp ut_array) {

              // Cteate a new tmp_array in the global scope
              // of the function.
              $tmp_array;

              // Create out array which will be returned
              $out_array = array();

              // Add the first row of $arr
              $out_array[] = $input_array[0];

              // For each array from the second array in $arr...
              //echo "<pre>",print_r ($input_array[0]),"</pre>";
              for ($row = 1; $row < sizeof($input_a rray); $row++) {

              // Create a new array
              $tmp_array = array();

              // Now, for each key in the current $arr row...
              for ($key_num = 0; $key_num < sizeof($input_a rray[$row]); $key_num+
              +) {

              // Add the values of $input_array[$row-1][$key_num] to the values
              // of the array in $output_array before ($out_array[$row]
              [$key_num])
              $tmp_array[$key_num] = $input_array[$row][$key_num] +
              $out_array[$row-1][$key_num];
              }
              // Append the $tmp_array to the $output_array
              $out_array[] = $tmp_array;
              }
              return $out_array;

              }

              $result= add_arrays($arr );
              echo 'Contents of $result: <br />';
              echo "<pre>Array 1:<br />",print_r($res ult[0]),"</pre>";
              echo "<pre>Array 2:<br />",print_r($res ult[1]),"</pre>";
              echo "<pre>Array 3:<br />",print_r($res ult[2]),"</pre>";
              ?>

              Comment

              • Daz

                #8
                Re: Adding arrays

                On May 7, 4:40 pm, dennis.spreng.. .@gmail.com wrote:
                Wow, thanks! It works great. I will spend the next hour or so trying
                to understand what code you wrote and why I still can't come up with
                stuff solutions that :-)
                >
                On 7 mei, 17:32, ZeldorBlat <zeldorb...@gma il.comwrote:
                >
                On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
                >
                Consider the following multi-dimensional array:
                ---------------------------
                $arr = array(
                array(3, 5, 7, 9),
                array(2, 4, 6, 8),
                array(1, 3, 5, 7)
                );
                >
                function add_arrays($arr ) {
                for ($row = 0; $row < count($arr[0]); $row++) {
                for ($column = 0; $column < count($arr[$column]); $column++) {
                $totals[$row] = $totals[$row] + $arr[$column][$row];
                }
                }
                print_r($totals );
                >
                }
                >
                add_arrays($arr );
                ---------------------------
                This returns the following array:
                Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
                >
                These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
                first elements), 12 (5 + 4 + 3, added all second elements), etc. This
                took me quite a portion of the day to construct ;) However, I would
                like add_arrays to return a multidimensiona l array like this:
                >
                Array (
                Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
                Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
                row (i.e. 5 = 3 + 2)
                Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
                row (i.e. 6 = 3 + 2 + 1)
                )
                >
                Could somebody please help me building a function that does just that?
                Adding one row to the previous one and adding the result to the output
                array? Any help would be greatly apprectiated!
                >
                Here's one way. Obviously there are others, also.
                >
                function add_arrays_cumu lative($arr) {
                $out = array();
                >
                for($i = 0; $i < count($arr); $i++) {
                if(!isset($out[$i]))
                $out[$i] = array();
                >
                for($j = 0; $j < count($arr[$i]); $j++)
                $out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
                $out[$i-1][$j] : 0);
                }
                >
                return $out;
                >
                }
                Oh, and for the record, ZeldorBlat's example is much safer. My example
                was primarily to help you understand one of the possible methods to
                achieve what you wanted. I didn't want you to just copy it and use it
                as it was. Hopefully my version will have helped you understand the
                steps taken to achieve the results you wanted. If you require a better
                explanation of anything (in ZeldorBlat's example, or my own), please
                don't hesitate to ask.

                Comment

                • dennis.sprengers@gmail.com

                  #9
                  Re: Adding arrays

                  @Daz - thanks! I will really help me understand. I will get back to
                  you if I'd have any questions :-)

                  Comment

                  • gosha bine

                    #10
                    Re: Adding arrays

                    dennis.sprenger s@gmail.com wrote:
                    Consider the following multi-dimensional array:
                    ---------------------------
                    $arr = array(
                    array(3, 5, 7, 9),
                    array(2, 4, 6, 8),
                    array(1, 3, 5, 7)
                    );
                    >
                    function add_arrays($arr ) {
                    for ($row = 0; $row < count($arr[0]); $row++) {
                    for ($column = 0; $column < count($arr[$column]); $column++) {
                    $totals[$row] = $totals[$row] + $arr[$column][$row];
                    }
                    }
                    print_r($totals );
                    }
                    >
                    add_arrays($arr );
                    ---------------------------
                    This returns the following array:
                    Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
                    >
                    These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
                    first elements), 12 (5 + 4 + 3, added all second elements), etc. This
                    took me quite a portion of the day to construct ;) However, I would
                    like add_arrays to return a multidimensiona l array like this:
                    >
                    Array (
                    Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
                    Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
                    row (i.e. 5 = 3 + 2)
                    Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
                    row (i.e. 6 = 3 + 2 + 1)
                    )
                    >
                    Could somebody please help me building a function that does just that?
                    Adding one row to the previous one and adding the result to the output
                    array? Any help would be greatly apprectiated!
                    >
                    hi, how about

                    function plus($a, $b) { return $a + $b; }

                    $res = array(array_shi ft($arr));
                    foreach($arr as $row)
                    $res[] = array_map('plus ', end($res), $row);


                    simple, huh? ;))



                    --
                    gosha bine

                    extended php parser ~ http://code.google.com/p/pihipi
                    blok ~ http://www.tagarga.com/blok

                    Comment

                    • Rami Elomaa

                      #11
                      Re: Adding arrays

                      ZeldorBlat kirjoitti:
                      On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
                      >Consider the following multi-dimensional array:
                      >---------------------------
                      >$arr = array(
                      > array(3, 5, 7, 9),
                      > array(2, 4, 6, 8),
                      > array(1, 3, 5, 7)
                      >);
                      >>
                      >function add_arrays($arr ) {
                      > for ($row = 0; $row < count($arr[0]); $row++) {
                      > for ($column = 0; $column < count($arr[$column]); $column++) {
                      > $totals[$row] = $totals[$row] + $arr[$column][$row];
                      > }
                      > }
                      > print_r($totals );
                      >>
                      >}
                      >>
                      >add_arrays($ar r);
                      >---------------------------
                      >This returns the following array:
                      >Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
                      >>
                      >These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
                      >first elements), 12 (5 + 4 + 3, added all second elements), etc. This
                      >took me quite a portion of the day to construct ;) However, I would
                      >like add_arrays to return a multidimensiona l array like this:
                      >>
                      >Array (
                      > Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
                      > Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
                      >row (i.e. 5 = 3 + 2)
                      > Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
                      >row (i.e. 6 = 3 + 2 + 1)
                      >)
                      >>
                      >Could somebody please help me building a function that does just that?
                      >Adding one row to the previous one and adding the result to the output
                      >array? Any help would be greatly apprectiated!
                      >
                      Here's one way. Obviously there are others, also.
                      >
                      function add_arrays_cumu lative($arr) {
                      $out = array();
                      >
                      for($i = 0; $i < count($arr); $i++) {
                      This is hair-splitting, but since count($arr) is static, it would save
                      some cpu cycles to store it in a variable instead of calling count() on
                      each iteration:

                      for($i = 0, $limit=count($a rr); $i < $limit ; $i++) {

                      Or better yet, iterate the array with foreach:

                      foreach($arr as $i =$row) {
                      if(!isset($out[$i]))
                      $out[$i] = array();
                      >
                      for($j = 0; $j < count($arr[$i]); $j++)
                      Same goes here.

                      I've gotten so used to foreach that I'm missing it a lot when I'm using
                      a language that hasn't got such a structure... It's so convinient.

                      --
                      Rami.Elomaa@gma il.com

                      "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
                      usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

                      Comment

                      • ZeldorBlat

                        #12
                        Re: Adding arrays

                        On May 7, 1:21 pm, Rami Elomaa <rami.elo...@gm ail.comwrote:
                        ZeldorBlat kirjoitti:
                        >
                        >
                        >
                        On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
                        Consider the following multi-dimensional array:
                        ---------------------------
                        $arr = array(
                        array(3, 5, 7, 9),
                        array(2, 4, 6, 8),
                        array(1, 3, 5, 7)
                        );
                        >
                        function add_arrays($arr ) {
                        for ($row = 0; $row < count($arr[0]); $row++) {
                        for ($column = 0; $column < count($arr[$column]); $column++) {
                        $totals[$row] = $totals[$row] + $arr[$column][$row];
                        }
                        }
                        print_r($totals );
                        >
                        }
                        >
                        add_arrays($arr );
                        ---------------------------
                        This returns the following array:
                        Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
                        >
                        These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
                        first elements), 12 (5 + 4 + 3, added all second elements), etc. This
                        took me quite a portion of the day to construct ;) However, I would
                        like add_arrays to return a multidimensiona l array like this:
                        >
                        Array (
                        Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
                        Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
                        row (i.e. 5 = 3 + 2)
                        Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
                        row (i.e. 6 = 3 + 2 + 1)
                        )
                        >
                        Could somebody please help me building a function that does just that?
                        Adding one row to the previous one and adding the result to the output
                        array? Any help would be greatly apprectiated!
                        >
                        Here's one way. Obviously there are others, also.
                        >
                        function add_arrays_cumu lative($arr) {
                        $out = array();
                        >
                        for($i = 0; $i < count($arr); $i++) {
                        >
                        This is hair-splitting, but since count($arr) is static, it would save
                        some cpu cycles to store it in a variable instead of calling count() on
                        each iteration:
                        >
                        for($i = 0, $limit=count($a rr); $i < $limit ; $i++) {
                        >
                        Or better yet, iterate the array with foreach:
                        >
                        foreach($arr as $i =$row) {
                        >
                        if(!isset($out[$i]))
                        $out[$i] = array();
                        >
                        for($j = 0; $j < count($arr[$i]); $j++)
                        >
                        Same goes here.
                        >
                        I've gotten so used to foreach that I'm missing it a lot when I'm using
                        a language that hasn't got such a structure... It's so convinient.
                        >
                        --
                        Rami.Elo...@gma il.com
                        >
                        "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
                        usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
                        Yes -- you're correct on both. In the normal course of things I would
                        save the value of count() and also use a foreach instead. Using
                        count() over and over again makes it a bit easier to follow the code,
                        and the OP was using regular for loops so I stuck with those.

                        In practice I use foreach almost exclusively -- and you're right in
                        that I really miss it in other languages :)

                        Comment

                        Working...