需要从嵌套底部向上处理数组
Need to work through an array from bottom of nest upwards
好的,我创建了一个与此类似的数组:
[household] => Array
(
[1234] => Array
(
[name] => 'A Nother'
[parent] =>
[bank] => Array
(
[balance] => 23745.98
[debit] => 14009
[trans] => 103
)
[children] => Array
(
[4567] => Array
(
[name] => 'M Claus'
[parent] => 1234
[bank] => Array
(
[balance] => 858
[debit] => 543
[trans] => 5
)
[children] =>
)
[7890] => Array
(
[name] => 'S Claus'
[parent] => 1234
[bank] => Array
(
[balance] => 1302
[debit] => 708
[trans] => 6
)
[children] =>
)
[2335] => Array
(
[name] => 'Elf 1'
[parent] => 1234
[bank] => Array
(
[balance] => 2315
[debit] => 1221
[trans] => 13
)
[children] => Array
(
[2896] => Array
(
[name] => 'Snowman'
[parent] => 2335
[bank] => Array
(
[balance] => 486
[debit] => 252
[trans] => 4
)
[children] =>
)
)
)
[1142] => Array
(
[name] => 'Grinch'
[parent] => 1234
[genone] => Array
(
[bank] => 2042
[debit] => 1212
[trans] => 12
)
[children] => Array
(
[8854] => Array
(
[name] => 'Fill ER Up'
[upline] => 1142
[bank] => Array
(
[balance] => 139
[debit] => 101
[trans] => 1
)
[children] =>
)
)
)
)
)
)
我的问题是我需要到达孙元素,处理它,然后向上遍历 parent 一直到根。但是,我需要提出每个 "leg" 并在处理相应的 parent 之前按顺序处理所有这些。
其中最多可以嵌套10次,每次处理的值都需要传入parent计算。所以在这个例子中,取所有 children 的余额,如果有意义的话,将其添加到 parent 的余额中以获得家庭余额,然后将这些金额传递给 grandparent 做一个家庭平衡。我能找到的所有东西都简单地反转数组,我可以硬编码,以便它从第三个嵌套开始,但我希望代码 未来证明 并且没有那种限制。我知道必须有一个优雅的解决方案,希望这里有人可以指点我 :) TIA。
这是一个基本的递归函数,它将完全满足您的要求。
function proccessArray($arrayData){
foreach($arrayData as $data){
if(count($data['children'])){
$childData = proccessArray($data['children']);
}
// do some calculations
return $calculatedData;
}
}
你不想从下往上遍历数组。树是做不到的。
相反,创建一个递归函数,并遍历每个人。
$householdBalance = 0;
$household = ...;
foreach ($array as &$person) {
// The "&" symbol is to allow us to modify the array. It creates a reference to $person.
$familyBalance = getBalanceOfFamily($person);
$householdBalance += $familyBalance;
// This will store the total balance which includes the person's children, grandchildren...
$person['family_balance'] = $familyBalance;
}
$household['balance'] = $householdBalance;
function getBalanceOfFamily(&$person) {
$familyBalance = $person['bank']['balance'];
// other calculations
foreach ($person['children'] as &$child) {
$balanceOfChildsFamily = getBalanceOfFamily($child);
$familyBalance += $balanceOfChildsFamily;
}
return $familyBalance;
}
这将遍历数组中的第一个人。然后,对于children,它会调用getBalanceOfFamily
,并且会return整个家庭的总余额。
好的,我创建了一个与此类似的数组:
[household] => Array
(
[1234] => Array
(
[name] => 'A Nother'
[parent] =>
[bank] => Array
(
[balance] => 23745.98
[debit] => 14009
[trans] => 103
)
[children] => Array
(
[4567] => Array
(
[name] => 'M Claus'
[parent] => 1234
[bank] => Array
(
[balance] => 858
[debit] => 543
[trans] => 5
)
[children] =>
)
[7890] => Array
(
[name] => 'S Claus'
[parent] => 1234
[bank] => Array
(
[balance] => 1302
[debit] => 708
[trans] => 6
)
[children] =>
)
[2335] => Array
(
[name] => 'Elf 1'
[parent] => 1234
[bank] => Array
(
[balance] => 2315
[debit] => 1221
[trans] => 13
)
[children] => Array
(
[2896] => Array
(
[name] => 'Snowman'
[parent] => 2335
[bank] => Array
(
[balance] => 486
[debit] => 252
[trans] => 4
)
[children] =>
)
)
)
[1142] => Array
(
[name] => 'Grinch'
[parent] => 1234
[genone] => Array
(
[bank] => 2042
[debit] => 1212
[trans] => 12
)
[children] => Array
(
[8854] => Array
(
[name] => 'Fill ER Up'
[upline] => 1142
[bank] => Array
(
[balance] => 139
[debit] => 101
[trans] => 1
)
[children] =>
)
)
)
)
)
)
我的问题是我需要到达孙元素,处理它,然后向上遍历 parent 一直到根。但是,我需要提出每个 "leg" 并在处理相应的 parent 之前按顺序处理所有这些。
其中最多可以嵌套10次,每次处理的值都需要传入parent计算。所以在这个例子中,取所有 children 的余额,如果有意义的话,将其添加到 parent 的余额中以获得家庭余额,然后将这些金额传递给 grandparent 做一个家庭平衡。我能找到的所有东西都简单地反转数组,我可以硬编码,以便它从第三个嵌套开始,但我希望代码 未来证明 并且没有那种限制。我知道必须有一个优雅的解决方案,希望这里有人可以指点我 :) TIA。
这是一个基本的递归函数,它将完全满足您的要求。
function proccessArray($arrayData){
foreach($arrayData as $data){
if(count($data['children'])){
$childData = proccessArray($data['children']);
}
// do some calculations
return $calculatedData;
}
}
你不想从下往上遍历数组。树是做不到的。
相反,创建一个递归函数,并遍历每个人。
$householdBalance = 0;
$household = ...;
foreach ($array as &$person) {
// The "&" symbol is to allow us to modify the array. It creates a reference to $person.
$familyBalance = getBalanceOfFamily($person);
$householdBalance += $familyBalance;
// This will store the total balance which includes the person's children, grandchildren...
$person['family_balance'] = $familyBalance;
}
$household['balance'] = $householdBalance;
function getBalanceOfFamily(&$person) {
$familyBalance = $person['bank']['balance'];
// other calculations
foreach ($person['children'] as &$child) {
$balanceOfChildsFamily = getBalanceOfFamily($child);
$familyBalance += $balanceOfChildsFamily;
}
return $familyBalance;
}
这将遍历数组中的第一个人。然后,对于children,它会调用getBalanceOfFamily
,并且会return整个家庭的总余额。