Replace last value in each row of a 2d array with the product of all values in the row excluding the last

77 Views Asked by At
Array ( 
[0] => Array ( [0] => 4 [1] => 3 [2] => 5 [3] => 7 [4] => 210 ) 
[1] => Array ( [0] => 4 [1] => 9 [2] => 5 [3] => 7 [4] => 210 ) 
[2] => Array ( [0] => 4 [1] => 9 [2] => 25 [3] => 7 [4] => 210 ) ) 

I would like to replace the last entry of each array with the product of the values from its preceding elements. For example,

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

Would actually be:

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

How do I delete that last element and multiply the preceding elements and fill in the correct value as the last element?

I tried reading about array_splice(), but do not understand the integer they used eg; array_splice($input, 1, -1);

3

There are 3 best solutions below

0
Pippo On

A compact solution: array_walk() processes every row of the main array, the callable receives the sub-array as reference, removes the last element, calculates the product of all remaining elements using array_reduce() and assigns it as the last element of the sub-array.

<?php

$mainArray = [
    
    [4, 3, 5, 7, 210],
    [4, 9, 5, 7, 210],
    [4, 9, 25, 7, 210],
];


array_walk ($mainArray, function (&$subArr) {
                           array_pop($subArr);
                           $subArr[] = !$subArr ? 0 : array_reduce($subArr, fn ($carry, $item) => $carry * $item, 1);
                        },
);


print_r ($mainArray);

Result:

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

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

    [2] => Array
        (
            [0] => 4
            [1] => 9
            [2] => 25
            [3] => 7
            [4] => 6300
        )
)
0
mickmackusa On

As you iterate the rows, isolate all but the last element and multiply them. Overwrite the last element with that product. (Demo)

var_export(
    array_map(
        function($row) {
            $row[array_key_last($row)] = array_product(array_slice($row, 0, -1));
            return $row;
        },
        $array
    )
);

Or multiply everything, then divide by the popped-off element: (Demo)

var_export(
    array_map(
        function($row) {
            $row[] = array_product($row) / array_pop($row);
            return $row;
        },
        $array
    )
);

Or remove the last element before multiplying (Demo)

var_export(
    array_map(
        function($row) {
            array_pop($row);
            $row[] = array_product($row);
            return $row;
        },
        $array
    )
);

Or enjoy arrow function syntax by uniting the product to the modified row: (Demo)

var_export(
    array_map(
        fn($row) => $row + [array_key_last($row) => array_product($row) / array_pop($row)],
        $array
    )
);

Or modify by reference using a classic loop: (Demo)

foreach ($array as &$row) {
    array_pop($row);
    $row[] = array_product($row);
}
var_export($array);
2
AudioBubble On

The array_splice() function in PHP is used to remove a portion of an array and replace it with something else.

Example given ,array_splice($input, 1, -1,[new_element])

$input: The input array.

1: The index at which to start the operation. In this case, it's 1, meaning the second element of each sub-array.

-1: The length of the portion to remove. A negative length means that the end of the array will be used as the length. So, -1 means everything from the start index to the end of the array.

[new_element]: Optional. An array containing the new element to insert into the array.

So, if you want to remove the last element of each sub-array and replace it with the product of the preceding elements, you could do something like this:

$arrays = [
    [4, 3, 5, 7, 210],
    [4, 9, 5, 7, 210],
    [4, 9, 25, 7, 210]
];

foreach ($arrays as &$array) {
    // Calculate the product of the first n-1 elements
    $product = array_product(array_slice($array, 0, -1));
    
    // Replace the last element with the product
    array_splice($array, -1, 1, $product);
}

// Output the modified arrays
print_r($arrays);

This code will produce the desired output, replacing the last element of each sub-array with the product of its preceding elements.