arrays - Inteligent subtraction of values in array (php)
问题描述
PHP Language
Is it difficult to write a function to subtraction value from each [Amount] values in array ?, all that until subtraction value is appeased ?.
Example what i need (before subtraction):
[MainStockAddedArr] => Array
(
[0] => Array
(
[Amount] => 10
[Price] => 19.44
)
[1] => Array
(
[Amount] => 15
[Price] => 15.55
)
[2] => Array
(
[Amount] => 20
[Price] => 11.55
)
[3] => Array
(
[Amount] => 30
[Price] => 11.10
)
)
Value to subtraction is for example 30 pieces. So i would need to have results in array look like this:
[MainStockAddedArr] => Array
(
[0] => Array
(
[Amount] => 0
[Price] => 19.44
)
[1] => Array
(
[Amount] => 0
[Price] => 15.55
)
[2] => Array
(
[Amount] => 15
[Price] => 11.55
)
[3] => Array
(
[Amount] => 30
[Price] => 11.10
)
)
In above example, two values are changed to 0 so that gives 25 pieces already been taken, on third value only 5 has been taken, that gives 30 pieces so no more is needed. This is just example, subtraction value will be dynamic, from a loop which is above this one.
In situation where subtraction value would be greater than in whole array (sum [Amounts]), else is needed.
解决方案
这是FIFO(先进先出)方法的示例。您可以尝试,如果需要也可以进行一些更正。
作为常规功能:
function fifo($argArray, $argValue, $colName = 'amount') {
// initialize
$total = is_numeric($argValue) ? $argValue : 0;
$i = 0;
// check for input amount (total)
if ($total > 0) {
// process the input array
foreach ($arrArray as $key => $v) {
// initialize amount value
$amount = $remains = $v[$colName] ?: 0;
// check for amount availability
if ($amount) {
// increment counter
$i++;
// calculate amount value remains
$remains -= $amount > $total ? $total : $amount;
// set amount value remains in array
$argArray[$key][$colName] = $remains;
// calculate last total remains
$total -= $amount - $remains;
// when total remains is zero then exit the loop process
if ($total === 0) break;
}
}
}
// return count of rows been used and total remains
return ['rowsCount' => $i, 'totalRemains' => $total];
}
用法:
$result = fifo(<argArray>, <argValue>, [<columnName>]);
作为一个班级:
namespace AnyNamespace;
class AnyClass extend \ArrayObject {
public function getFifo($argValue, $colName = 'amount') {
return $this::fifo($this, $argValue, $colName);
}
public static function fifo($argArray, $argValue, $colName = 'amount') {
// initialize
$total = is_numeric($argValue) ? $argValue : 0;
$i = 0;
// check for input amount (total)
if ($total > 0) {
// process the input array
foreach ($arrArray as $key => $v) {
// initialize amount value
$amount = $remains = $v[$colName] ?: 0;
// check for amount availability
if ($amount) {
// increment counter
$i++;
// calculate amount value remains
$remains -= $amount > $total ? $total : $amount;
// set amount value remains in array
$argArray[$key][$colName] = $remains;
// calculate last total remains
$total -= $amount - $remains;
// when total remains is zero then exit the loop process
if ($total === 0) break;
}
}
}
// return count of rows been used and total remains
return ['rowsCount' => $i, 'totalRemains' => $total];
}
}
用法:
use AnyNamespace;
...
$result = AnyClass::fifo(<argArray>, <argValue>, [<columnName>]);
或者
use AnyNamespace;
...
$anyClass = new AnyClass(<inputArray>);
$result = $anyClass->getFifo(<argValue>, [<columnName>]);
推荐阅读
- api - Lumen 仅返回请求中指定的字段
- python - 如何“最好”检查 csv 文件中是否出现字符串/单词
- javascript - 需要 IE 填充程序的 Angular 5 和 IE11 问题
- vba - VBA Query returning nulls
- python - { "detail": "方法 \"GET\" 不允许。" }
- c++ - CMAKE Boost 单元测试框架在 Mac 上不起作用
- javascript - 如果所有输入字段不为空,则播放视频
- java - 在 iOS-Objective C 上签名的数据在 Java 中验证时总是返回 false
- visual-studio - Visual Studio 中的这个框叫什么,如何禁用它?
- php - SQL 从表中获取值