sum two arrays by indexes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerard Matthew

    sum two arrays by indexes

    I'm looking for a short way of suming the indexes of two arrays and
    returning one array as the result. I'm not referring to what
    array_sum() is capable of doing. Below will get it done; but is there
    any built in function doing the same??

    function sum_by_index($a rray1,$array2) {
    for ($i = 0; $i <= (count($array1) -1); $i++) {
    $temp[$i] = $array1[$i] + $array2[$i];
    }

    return $temp;
    }//note both $array1 and $array2 are to be of the same size


    Regards,
    Gerard.

  • Ken Robinson

    #2
    Re: sum two arrays by indexes

    Gerard Matthew wrote (in part):[color=blue]
    > I'm looking for a short way of suming the indexes of two arrays and
    > returning one array as the result. I'm not referring to what
    > array_sum() is capable of doing. Below will get it done; but is there
    > any built in function doing the same??
    >
    > function sum_by_index($a rray1,$array2) {
    > for ($i = 0; $i <= (count($array1) -1); $i++) {
    > $temp[$i] = $array1[$i] + $array2[$i];
    > }
    >
    > return $temp;
    > }//note both $array1 and $array2 are to be of the same size[/color]

    The following function does the same as yours with the added
    functionality that it works with associative arrays also:

    function sum_by_index($a ry1,$ary2)
    {
    $tmp = array();
    foreach($ary1 as $k=>$v) $tmp[$k] = $v + $ary2[$k];
    return($tmp);
    }

    In your function, why do you use

    for ($i = 0; $i <= (count($array1) -1); $i++)

    when

    for ($i = 0; $i < count($array1); $i++)

    works just as well (and almost everyone else uses it).

    Ken

    Comment

    • ZeldorBlat

      #3
      Re: sum two arrays by indexes


      Gerard Matthew wrote:[color=blue]
      > I'm looking for a short way of suming the indexes of two arrays and
      > returning one array as the result. I'm not referring to what
      > array_sum() is capable of doing. Below will get it done; but is there
      > any built in function doing the same??[/color]

      Not that I know of.
      [color=blue]
      > function sum_by_index($a rray1,$array2) {
      > for ($i = 0; $i <= (count($array1) -1); $i++) {
      > $temp[$i] = $array1[$i] + $array2[$i];
      > }
      >
      > return $temp;
      > }//note both $array1 and $array2 are to be of the same size
      >[/color]

      Nothing wrong with that. Although it might be slightly more efficient
      as such:

      function sum_by_index(&$ array1, &$array2) {
      for($i = (count($array1) - 1); $i >= 0; $i++)
      $temp[$i] = $array1[$i] + $array[$i];
      return $temp;
      }

      Comment

      • Chung Leong

        #4
        Re: sum two arrays by indexes

        If you want to look clever by baffling others:

        $sum = array_map('arra y_sum', array_map(null, $array1, $array2));

        Comment

        Working...