【发布时间】:2010-10-25 08:26:10
【问题描述】:
如果不清楚,请耐心等待;我很难完全解决这个问题(因此我在这里寻求帮助)。
我有一个如下所示的数组:
Array
(
[DimA1] => Array
(
[DimB1] => Array
(
[DimC1] => Array
(
[value1] => 13708
[value2] => 4.5
)
[DimC2] => Array
(
[value1] => 1846
[value2] => 15.8
)
)
[DimB2] => Array
(
[DimC1] => Array
(
[value1] => 18166
[value2] => 6.4
)
)
[DimA2] => Array
(
....... etc
我需要遍历这个数组,当我到达 value1 和 value2 时,我需要做一些数据库插入。在这个时间点上,我需要知道我当前正在单步执行哪些数组,并将它们的键名用作我的数据库插入的一部分。
我目前的解决方案是这样的:
public function recurseCounts($array,$dims = array()) {
foreach ($array as $key => $value) {
$dims[] = $key;
if (isset($value['value1']) || isset($value['value2'])) {
print_r($value); // For debugging...
print_r($dims); // For debugging...
// DB Logic to insert dimensions in to DB here
// DB Logic to insert values in to DB here
array_pop($dims);
} else {
$this->recurseCounts($value,$dims);
}
}
}
这一直有效,直到循环到达 DimB2,这就是事情开始变得古怪的地方。
关于如何解决这个问题的任何想法?
【问题讨论】: