muliply array elements

Discussion in 'PHP' started by bumbarov, Nov 17, 2011.

  1. #1
    Hello!

    I have a question about multiply array elements.

    if $myarr = array (2, 3, 4);

    how to get results 2 * 3 * 4 with FOREACH ?

    Thanks!
     
    Solved! View solution.
    bumbarov, Nov 17, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Set a variable equal to 1 then for each element in the array multiple that variable by it.
     
    plog, Nov 17, 2011 IP
  3. #3
    
    <?php
    $result = 1;
    $myarr = array (2, 3, 4);
    foreach ($myarr as $i) {
    	$result *= $i;
    }
    echo $result;
    ?>
    
    PHP:
     
    xrvel, Nov 17, 2011 IP