437,614 Members | 1,674 Online Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

 P: n/a 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); \$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 (  =6  =12  =18  =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 multidimensional array like this: Array ( Array (  =3  = 5  = 7  = 9 ) // 1st row Array (  =5  = 9  =13  =17 ) // 1st row + second row (i.e. 5 = 3 + 2) Array (  =6  =12  =18  =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! May 7 '07 #1
11 Replies

 P: n/a 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! May 7 '07 #5

 P: n/a 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

 P: n/a 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 ",print_r(\$input_array),""; for (\$row = 1; \$row < sizeof(\$input_array); \$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_array[\$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:
'; echo "
Array 1:
",print_r(\$result),"
"; echo "
Array 2:
",print_r(\$result),"
"; echo "
Array 3:
",print_r(\$result),"
"; ?> May 7 '07 #7

 P: n/a 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

 P: n/a @Daz - thanks! I will really help me understand. I will get back to you if I'd have any questions :-) May 7 '07 #9 