数组中值的智能减法(php)
Inteligent subtraction of values in array (php)
PHP 语言
是否很难编写一个函数来从数组中的每个 [Amount] 值中减去值?直到减法值被平息为止?。
我需要的示例(减法前):
[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
)
)
要减去的值例如是 30 件。所以我需要在数组中得到这样的结果:
[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
)
)
在上面的例子中,两个值被更改为 0,这样就给出了 25 件已经被拿走,第三个值只有 5 已经被拿走了,这给出了 30 件所以不需要更多。这只是示例,减法值将是动态的,来自高于此循环的循环。
在减法值大于整个数组(总和 [Amounts])的情况下,需要其他。
这里是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>]);
作为 class:
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>]);
PHP 语言
是否很难编写一个函数来从数组中的每个 [Amount] 值中减去值?直到减法值被平息为止?。
我需要的示例(减法前):
[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
)
)
要减去的值例如是 30 件。所以我需要在数组中得到这样的结果:
[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
)
)
在上面的例子中,两个值被更改为 0,这样就给出了 25 件已经被拿走,第三个值只有 5 已经被拿走了,这给出了 30 件所以不需要更多。这只是示例,减法值将是动态的,来自高于此循环的循环。
在减法值大于整个数组(总和 [Amounts])的情况下,需要其他。
这里是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>]);
作为 class:
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>]);