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.
C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.
The bcmul() function in PHP is an inbuilt function and is used to multiply two arbitrary precision numbers. This function accepts two arbitrary precision numbers as strings and returns the multiplication of the two numbers after scaling the result to a specified precision.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With