array_product

  • We can get the product of an array of integers using a combination of Ruby’s Enumerable#inject method with Fixnum#*.

    PHP

    $numbers = array(2, 2, 2, 2);
    $result = array_product($numbers);
    print $result;
    // => 16

    Ruby

    result = [2, 2, 2, 2].inject {|product, element| product * element }
    puts result
    # => 16

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.