Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
在这里,尝试这个 - 我确信它既不是完美也不是最佳解决方案( 3 foreach-es ),但它适用于相当大的数据集...
$inputArray 是包含您提供的数据的多维数组,顺便说一句..编辑
:固定版本:
$result = array(); foreach( $inputArray as $subArray ) { foreach ( $subArray as $subKey => $member ) { if ( empty( $result[$subKey]) ) { $result[$subKey] = $member; } else { foreach ( $member as $id => $subMember ) { if ( empty( $result[$subKey][$id]) ) { $result[$subKey][$id] = $subMember; } else { $result[$subKey][$id] += $subMember; } } } } }
编辑2:由于您更改了数组的格式,因此解决方案有所不同:
注意: $array1 和 $array2 是您的“全局”-预定义数组。
$arrayWrapper = array_merge( ( array ) $array1, ( array ) $array2 ); $result = array(); foreach ( $arrayWrapper as $inputArray ) { foreach( $inputArray as $subArray ) { foreach ( $subArray as $subKey => $member ) { if ( empty( $result[$subKey]) ) { $result[$subKey] = $member; } else { foreach ( $member as $id => $subMember ) { if ( empty( $result[$subKey][$id]) ) { $result[$subKey][$id] = $subMember; } else { $result[$subKey][$id] += $subMember; } } } } } }
用你的数据测试了一下,应该可以。
干杯。
Here, try this - I'm sure it's neither a perfect nor optimal solution ( 3 foreach-es ), but it works on a reasonably large data set...
$inputArray is the multidimensional array with the data you provided, btw...
EDIT: Fixed version:
EDIT2: Since you changed the format of arrays, the solution is different:
Note: $array1 and $array2 are your "global" - predefined arrays.
Tested it with your data, should work.
Cheers.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(1)
在这里,尝试这个 - 我确信它既不是完美也不是最佳解决方案( 3 foreach-es ),但它适用于相当大的数据集...
$inputArray 是包含您提供的数据的多维数组,顺便说一句..编辑
:固定版本:
编辑2:由于您更改了数组的格式,因此解决方案有所不同:
注意: $array1 和 $array2 是您的“全局”-预定义数组。
用你的数据测试了一下,应该可以。
干杯。
Here, try this - I'm sure it's neither a perfect nor optimal solution ( 3 foreach-es ), but it works on a reasonably large data set...
$inputArray is the multidimensional array with the data you provided, btw...
EDIT: Fixed version:
EDIT2: Since you changed the format of arrays, the solution is different:
Note: $array1 and $array2 are your "global" - predefined arrays.
Tested it with your data, should work.
Cheers.