[英]Accumulative sum in nested array PHP (recursive)
I have an array which has several levels (not defined or defined on-the-fly), and an associative key "num_products" (number of products). 我有一个数组,该数组具有多个级别(未定义或即时定义),以及一个关联键“ num_products”(产品数量)。 After the array is created, I want another associative key "total", which is initialized to zero, to add all "num_products" which are at that level and below ( recursively ).
创建数组之后,我希望另一个关联键“ total”(初始化为零)添加该级别及以下 级别的所有“ num_products”( 递归 )。 For example:
例如:
$array = array ( "1" =>
array ( "id" => 1, "num_products" => 3, "total" => 0,
"sublevel" => array( "id" => 2, "num_products" => 5, "total" => 0) );
Here I would need the first total to account for ( 5 + 3 = 8 ) and the second total for 5. 在这里,我需要第一个总数考虑(5 + 3 = 8),第二个总数考虑5。
I have tried with a recursive function, and passing the array by reference in order to modify the values, but I cannot achieve what I want, as I only get one upper level. 我尝试使用递归函数,并通过引用传递数组以修改值,但是我无法实现我想要的功能,因为我只能得到一个更高的级别。 Example:
例:
sum_array( $array, null);
function sum_array(&$array, &$father){
foreach($array as $key => $item){
$array[$key]["total"] += $array[$key]["num_products"];
$father["total"] += $array[$key]["num_products"];
totales_array($array[$key]["sublevel"], $array[$key]);
}
}
Try USING RECURSIVE FUNCTION Quick Test Here 在这里尝试使用递归功能 快速测试
<?php
$array = array ( "1" =>
array ( "id" => 1, "num_products" => 3, "total" => 0,
"sublevel" => array( "id" => 2, "num_products" => 5, "total" => 0),
"sublevel2" => array( "id" => 6, "num_products" => 7, "total" => 0),
"sublevel3" => array( "id" => 15, "num_products" => 10, "total" => 0),
),
"2" =>
array ( "id" => 3, "num_products" => 7, "total" => 0,
"sublevel" => array( "id" => 4, "num_products" => 9, "total" => 0) ));
function getGroupTotal(&$array, &$total=0, $clear=0){
if(isset($array['num_products']) && !$clear){
$total += $array['num_products'];
$clear = 1;
}
foreach($array as $key=>$data){
if(isset($data['num_products'])){
$total += $data['num_products'];
}
if(is_array($data)){
getGroupTotal($data, $total, $clear);
}
}
return $total;
}
function buildTotalProducts(&$array, &$total=0, &$init=[], $clear=0, &$gTotal=[]) {
if(!$clear){
$gTotal = [];
foreach ($array as $key => &$data) {
$data['total'] = getGroupTotal($data);
$gTotal[] = $data['total'];
}
$clear = 1;
}
$y = 0;
foreach($array as $key=>&$children){
if(empty($init)){ $init = [0=>$key, 1=>$array];}
if(is_array($children)){
$numProd = isset($children['num_products'])? $children['num_products']:0;
$k = &$children;
if($clear==1){
$k['total'] = $gTotal[$y];
$gTotal[$y] -= $numProd;
$numProd = 0; $clear=2;
}else {
$k['total'] = ($gTotal[$y] == 0)?$numProd : $gTotal[$y];
$gTotal[$y] -= $numProd;
}
if($init[1][$init[0]] == $array[$key]){
$y++;
}
buildTotalProducts($children, $total, $init, $clear, $gTotal);
}
}
return $array;
}
var_dump( buildTotalProducts($array) );
// YIELDS::
array (size=2)
1 =>
array (size=6)
'id' => int 1
'num_products' => int 3
'total' => int 25
'sublevel' =>
array (size=3)
'id' => int 2
'num_products' => int 5
'total' => int 22
'sublevel2' =>
array (size=3)
'id' => int 6
'num_products' => int 7
'total' => int 17
'sublevel3' =>
array (size=3)
'id' => int 15
'num_products' => int 10
'total' => int 10
2 =>
array (size=4)
'id' => int 3
'num_products' => int 7
'total' => int 16
'sublevel' =>
array (size=3)
'id' => int 4
'num_products' => int 9
'total' => int 9
It is a double recursive function. 它是一个双重递归函数。
<?php
$array = array();
$array2 = array();
$array3 = array();
$array8 = array();
$array16 = array();
$array16[] = array("id" => 41, "num" => 1, "total" => 0, "sublevel" => array() );
$array24 = array();
$array24[] = array("id" => 101, "num" => 14, "total" => 0, "sublevel" => array() );
$array3[] = array("id" => 7, "num" => 10, "total" => 0, "sublevel" => array());
$array3[] = array("id" => 31, "num" => 15, "total" => 0, "sublevel" => $array16 );
$array2[] = array ( "id" => 3, "num" => 0, "total" => 0, "sublevel" => $array3 );
$array2[] = array( "id" => 10 , "num" => 2, "total" => 0, "sublevel" => array( array("id" => 11, "num" => 3, "total" => 0, "sublevel" => $array24)));
$array2[] = array("id" => 111, "num" => 5, "total" => 0, "sublevel" => array());
$array[] = array ( "id" => 1, "num" => 2, "total" => 0, "sublevel" => $array2 );
$array5[] = array("id" => 27, "num" => 3, "total" => 0, "sublevel" => array() );
$array[] = array("id" => 2, "num" => 2 , "total" => 0, "sublevel" => $array5 );
echo "<pre>"; print_r($array); echo "</pre>";
$parent = null;
recorrer_niveles($array, -1, $parent, $array);
echo "<pre>"; print_r($array); echo "</pre>";
function recorrer_niveles(&$array, $nivel, &$parent, &$original){
$nivel++;
foreach($array as $key => $item){
$cantidad = $array[$key]["num"];
$array[$key]["total"] += $cantidad;
$cuenta = count($parent);
for($i=$nivel;$i<$cuenta;$i++){
unset($parent[$i]);
} // for
sum_original($original, $parent, $array[$key]["num"]);
$parent[$nivel ] = $array[$key]["id"];
recorrer_niveles($array[$key]["sublevel"], $nivel, $parent, $original);
} // foreach
} // function
function sum_original(&$original ,$parent, $cantidad){
if(!is_array($parent)) return 0;
foreach($original as $key => $value){
if(isset($original[$key]["id"]) && in_array($original[$key]["id"], $parent)){
$original[$key]["total"] += $cantidad;
} // if
sum_original($original[$key]["sublevel"], $parent, $cantidad);
} // foreach
} // function
?>
See the code running: https://eval.in/641847 查看运行的代码: https : //eval.in/641847
try this: 尝试这个:
$array = array ( "1" => array ( "id" => 1, "num_products" => 3, "total" => 0, "sublevel" => array( "id" => 2, "num_products" => 5, "total" => 0) ) );
function sum_recursive($array) {
$sum = 0;
foreach( $array as $key=>$value ) {
if( isset($value['sublevel']) && is_array($value['sublevel']) && count($value['sublevel'])>0 ) {
if( isset($value['num_products']) ) {
$sum += $value['num_products'];
}
$sum += sum_recursive($value);
} else {
if( isset($value['num_products']) ) {
$sum += $value['num_products'];
}
}
}
return $sum;
}
$sum = sum_recursive($array);
echo "Sum is: ".$sum;
Sum is: 8 总和是:8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.