我有一个名为$times
的数组。它是一个小数字列表 (15,14,11,9,3,2
)。这些将由用户提交,应该是分钟。由于 PHP 时间以秒为单位,我想将数组的每个元素乘以 60。
我一直在尝试使用 array_walk
和 array_map
但我无法让它们正常工作。
最佳答案
您可以使用 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()
用于回调的 lambda 函数示例:
array_map(function($el) { return $el * 60; }, $input);
同样适用于 PHP < 5.3
array_map(create_function('$el', 'return $el * 60;'), $input);
或用bcmul
回调
array_map('bcmul', $input, array_fill(0, count($input), 60));
但是为此使用 foreach
也没有错。
关于php - 将平面数组中的每个整数乘以 60,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2660152/