Subtract a number from array starting backwards from tail ([1,2,3] - 4 = [1,1,0])

Helen Che

I have an array and I want to subtract a number "n" starting from the last index, some examples:

[1,2,3,4] - 2 = [1,2,3,2]
[1,2,3,4,0,0] - 3 = [1,2,3,1,0,0]
[1,2,3,4,0] - 6 = [1,2,1,0,0]

The subtracting number will never be greater than the sum of array values.

Currently I'm using a solution for fixed array sizes with a trivial loop and subtraction. How would I approach this for a dynamic size? What PHP methods will make this easier?

pbaldauf

I've prepared a small sample code which should work:

$arr1 = array( 1,2,3,4); //-2
$arr2 = array( 1,2,3,4,0,0); //-3
$arr3 = array( 1,2,3,4,0,); //-6


array_subtract( $arr1, 2 );
array_subtract( $arr2, 3 );
array_subtract( $arr3, 6 );

print_r($arr1);
print_r($arr2);
print_r($arr3);

//heres a handy function which requires the array and the subtraction value as input 
function array_subtract( &$array, $sub ) { 
    $size = sizeof( $array ) - 1;

    for( $i = $size; $i >= 0; $i-- ) :
        if ($sub > 0 ) :
            if ( $array[$i] > $sub ) :
                $array[$i] -= $sub;
                $sub = 0;
            else : 
                $sub -= $array[$i];
                $array[$i] -= $array[$i];
            endif;
        else :
            break;
        endif;
    endfor;    
}

The print_r output looks like

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 2
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 1
    [4] => 0
    [5] => 0
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 1
    [3] => 0
    [4] => 0
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I want to write function, which take input as a number and returns array , starting from 1 to number. example N= 4, output = [1,2,3,4]

Subtract number from array

program to exclude number having apart from 1,2,3

Excel VBA Array from Range Starting at -1 rather than 0

convert array to object in javascript (starting from 1 instead of 0)

Split the values from ('1,2,3') to ('1','2','3') or (1,2,3) in sql

Subtract 1 letter and number from multiple strings of letters and numbers

from a stack a number of options as 1,2,3 is given by user and we need to perform operations as specified

How do I know if a number can be obtained by incrementing by 4 starting from 1?

Subtract a different number from each column in an array

How to subtract the same number from an array

How to subtract every value of one array from another array to find location of where value of array 1 minus value of array 2 = 0?

Serial NO starting from 1

How do I subtract 1 from an array item in BASH?

How do I subtract 1 from an array of integers

Polars subtract numpy 1xn array from n columns

How to subtract from a starting amount?

Order an unordered array of numbers from 1-8, so that the end and starting integers are alternated eg [8,1,7,2,6,3,5,4,]

Jquery - adding number to each iteration starting from 1

Make all combinations of size k starting from 1 to number n

Starting page number from 1 after an odd section break

Subtract 3 lists in a tuple from 1 tuple

Subtract number from enum

Subtract a number from a variable

subtract from number IF statment

How to subtract list 1 from list 2?

Is it a JMeter bug if ctx.getThreadNum() returns number starting from 0 instead of 1?

Array from integer: 143 -> [1, 4, 3]

How to subtract 1d array from 2d array's rows in Numpy