How can I multiply each item in an array easily with PHP?

multiply each element of an array by a number in java
php multiply two arrays
php array map
multiply each number in an array javascript
how to multiply each number in an array
multiplication of two 1d array in c
multiplication of array in c++
product of array elements java

I have an array called $times. It is a list of small numbers (15,14,11,9,3,2). These will be user submitted and are supposed to be minutes. As PHP time works on seconds, I would like to multiply each element of my array by 60.

I've been playing around with array_walk and array_map but I can't get those working.

Just iterate over the array with the foreach statement and multiply:

foreach ($times as $value) {

  $new_times[] = $value * 60;


array_product - Manual, This function can be used to test if all values in an array of booleans are TRUE. testing an indefinite number of booleans and is easy to construct in a loop. Every array and hash in Ruby is an object, and every object of these types has a set of built-in methods. Programmers new to Ruby can learn about how to use the each method with an array and a hash by following the simple examples presented here.

You can use array_map:

array_map() returns an array containing all the elements of arr1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

Examples with lambda functions for callbacks:

array_map(function($el) { return $el * 60; }, $input);

Same for PHP < 5.3

array_map(create_function('$el', 'return $el * 60;'), $input);

Or with bcmul for callback

array_map('bcmul', $input, array_fill(0, count($input), 60));

But there is nothing wrong with just using foreach for this as well.

Program for multiplication of array elements, We are given an array and we have to calculate the product of an array using both iterative and We traverse array from left to right and multiply elements with result. PHP. filter_none. edit close. play_arrow. link brightness_4 code Get hold of all the important DSA concepts with the DSA Self Paced Course at a� However, because each array element is an object, each must be created with the keyword new before it can be used. The steps for working with an array of objects are: 1. Declare the array 2. Create the array 3. Create each object in the array These steps are translated into code in the following example.

You may want to use foreach for simplicity in your case:

foreach( $times as &$val ){ $val *= 60; }

I assume that the values of your array are the ones you want to multiply, not the keys. As the above solution uses references, it will also alter your original array - but as you were aiming at array_map before, I think that you want that.

The solution above is probably easier to understand and most likely faster than using array_map which (obviously) has to call a function for each element. I'd use array_map only for more complicated things such as advanced sorting algorithms and so on, but definitely not for something as trivial as this.

PHP array_product() Function, Well organized and easy to understand Web building tutorials with lots of examples PHP Array Reference The array_product() function calculates and returns the product of an array. Return Value: Returns the product as an integer or float reviewed to avoid errors, but we cannot warrant full correctness of all content. The task is to count the number of different array that can be formed of size n such that each element is between 1 to k and two consecutive element are different. Also, the first and last elements of each array should be 1 and x respectively. Examples : Input : n = 4, k = 3, x = 2 Output : 3

I'd personally second the suggestion from Gordon to just use a lambda (or created) function and either do:

array_map(function($el) { return $el * 60; }, $input_array);

(PHP >= 5.3) or

array_map(create_function('$el', 'return $el * 60;'), $input_array);

(PHP < 5.3)

Definitely I see no reasons for duplicating the array (can become cumbersome if lot of values are involved); also, pay attention that using foreach (which I second can be handy) can also be dangerous if you're not working with it carefully ...and then maintenance can become daunting anyways (because you have to remember to deal with it every time you work on that code). If you have no reasons for optimizing at this stage (IE your application has not problems of speed), don't do it now and don't worry about using array_map. You can think about ease of maintenance now and optimize later, in case you really need to.

In fact, if you go by reference and then you use foreach again, you might step into unexpected results (see example below...)


foreach ($a as &$v) {
        $v *= 60;
foreach ($a as $v);

Output is:

Array ( [0] => 60 [1] => 120 [2] => 180 )

Array ( [0] => 60 [1] => 120 [2] => 120 )

Probably not what you expect on the second cycle. This is why I usually avoid the foreach & byref combo when I can.

How to multiply corresponding elements of two lists in php?, The technique which I am going to show you here is really very easy but How to multiply each column of every row by chosen value in php? How to How to multiply corresponding elements of two arrays of integers in php? OK, so how do we multiply two matrices? In order to multiply matrices, Step 1: Make sure that the the number of columns in the 1 st one equals the number of rows in the 2 nd one. (The pre-requisite to be able to multiply) Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix.

from PHP 5.1 there is function called array_product(). You can use it.

$mul = array_product([1, 2, 3]);
echo $mul;  // 6

How to multiply each element of a list by a number in Python, array(object) with object as a list to convert object to a NumPy array . Use the syntax array * number with array as the previous result to multiply each element in� The example of using single array. If you use a single array in the SUMPRODUCT function then it will simply sum the array items. In the following example, only the price column range is given in the SUMPRODUCT function: SUMPRODUCT formula: =SUMPRODUCT(B2:B7) The example of getting the count by SUMPRODUCT function. The SUMPRODUCT function is

How to multiply each element of an array with a number using a , Original question: How do I multiply each element of an array with a number using a Array seems to be hard in C programming, How can I learn it easily? In order to apply array_map with callback checking for localised values like city name, country name, you have to provide some sort of comparison array. Here's an example of array_count_values for Polish city names. Just switch array_keys / array_values in order to obtain lowercase / uppercase.

Python Exercise: Multiply all the numbers in a list, home Front End HTML CSS JavaScript HTML5 php.js Write a Python function to multiply all the numbers in a list. Sample Solution:- Python Code: def multiply(numbers): total = 1 for x in numbers: total *= x return total Leave out the stop = an implicit end to the very last item >>> a[9:] [18, 20] >� PHP array functions. PHP provides a wide range of functions for working with arrays. We won't go into them in detail, we'll just show you a list of the most important ones. Each function references the corresponding page in the official PHP manual where it is described and shown in examples.

Multiply elements of 2 arrays together into another array, Compute array Z by multiply Xi * Yi then compute the square-root of this computation. What I have Which you may find easier to read. Add column and multiply its result to all elements of another column Tagged: arrays, php, shell scripts. This can be easily done by applying a loop and performing an operation to each element. But having shorthands to solve this problem is always beneficial and helps to focus more on important aspects of problem. Let’s discuss certain ways in which each element of list can be operated. Method #1 : Using list comprehension

  • I would go for the array_map / bcmul solution Gordon provided if you don't want to use foreach or custom functions yet, have you considered not multiplying the array by 60 and instead divide the PHP time by 60?
  • I am going to try all of the examples, just so I know how to do it in future. The foreach loop seems to be the most simple. What is the difference between *= and *?
  • Henryz: $a *= 6 is the short form of $a = $a * 6
  • doesn't work in PHP 7.2.6 anymore: " An iterator cannot be used with foreach by reference "
  • Hi, thanks for the answer, this only seemed to work if it built a new array like this v=[]. Not sure why :S
  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. - From review