Sub-array item add

//problem : add item three of each sub-array to 1st item of next array ,
// then place sum in 1st psition of next sub-array

//  this is input array

            $input_Arr = [ [ 1, 2 ,4 ], [ 4, 2, 6] , [ 2, 8, 2 ] ];

            //1st iteration

            //[ 1 , 2 , 4 ] , [ 4 , 2 ,6 ]        4+4 = 8 , place 8 at 1st psition in 2nd array  [ 8 , 2 , 6 ]  ( this array will be used in calculation of next iteration )

            //then 2nd iteration

            //[ 8, 2, 6 ]  , [ 2 , 8  ,2 ]         6 + 2 = 8 , place 8 at 1st psition  in next array [ 8, 8, 2 ]

//  this is result array

$result_Arr = [ [1, 2, 4 ], [8, 2, 6], [ 8, 8, 2 ] ];

echo $result_Arr;

thnks to guys ho will solve this .

What is the real problem you are trying to solve by doing this?

Think about the keys in the input array structure. If you are not sure print out the array.

echo "<pre>";
print_r($input_Arr); 
echo "</pre>";	

You will see primary keys holding sub arrays of key => value pairs.
IF you were to loop through this array using foreach() defining the primary key as $k and the sub array as $ar you could then just copy the array using those variables. Remember $ar is holding that sub array of key => value pairs.

$result_Arr[$k] = $ar;

You can print_r() the result array to check.

Now you didn’t want that first sub array modified and you’ll see that this sub array has a primary key of 0. As this is the only sub array we want copied, we should write an IF condition to only define the result array when the primary key is empty (zero). Now for those other primary keys, we will handle and contained them in an }else{ statement, which we can add now.

if(empty($k)){
	$result_Arr[$k] = $ar;
}else{

}

If you notice, those sub arrays have the keys (0, 1, 2) and using a combination of the primary and secondary sub array keys you can get any value. for example. echo $input_Arr[1][2]; would show 6.

Now, in the foreach loop we’ve defined $k and $ar and we know we are not changing values for sub arrays 1 and 2 so they can be directly transferred to the results array and placed within the }else{ statement.

$result_Arr[$k][1] = $ar[1];
$result_Arr[$k][2] = $ar[2];

We then come to that OP problem of adding the last value of the previous sub array (key [2]) to the first value of the current sub array (key [0]). As $k represents the current primary key in this foreach loop, then the previous primary key would be $k-1 assuming this is a natural array and the key is greater than zero. Using this and the sub array key for the last item, you can get the value for that last item.

$input_Arr[$k-1][2]

To this we want to add the value of the first item in the current sub array using the sub key of [0] like so.

$input_Arr[$k][0]

Now we put these into a mathematical statement and set the result as the value for the first sub key [0] in the results array also within the }else{ statement. Note place it as the first item as the sub key is 0.

$result_Arr[$k][0] = $input_Arr[$k-1][2] + $input_Arr[$k][0];

All in all

echo "<pre>";
print_r($input_Arr); 
echo "</pre>";	
 
$result_Arr = array();
if(!empty($input_Arr)):	
	foreach($input_Arr as $k => $ar):		
		if(empty($k)){
			$result_Arr[$k] = $ar;
		}else{		
			$result_Arr[$k][0] = $input_Arr[$k-1][2] + $input_Arr[$k][0];	
			$result_Arr[$k][1] = $ar[1];
			$result_Arr[$k][2] = $ar[2];			
		}
	endforeach;
endif;

echo "<pre>";
print_r($result_Arr); 
echo "</pre>";

Isn’t that a little bit oversized?

I would simply do

for($index = 0; $index < count($input_array) - 1; $index++)
{
    $input_array[$index + 1][0] += $input_index[$index][2];
}

If really needed to preserve the input array you can clone it to result_array enforce the loop

Nice approach.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.