skip to Main Content
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);

2

Answers


  1. 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
            )
    )
    
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search