tags:

views:

159

answers:

2

Bear with me if this is unclear; I have trouble fully wrapping my head around this (hence why I am here to ask for help).

I have an array that looks like this:

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

I need to step through this array and when I get to value1 and value2, I need to do some database inserts. At this point in time, I need to be aware of just which arrays I'm currently stepped through and use their key name as part of my database inserts.

My current solution looks like this:

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);
  }
 }
}

This works up until the point the loop hits DimB2, that's where things start getting whacky.

Any ideas on how to solve this?

+1  A: 

You need to keep track of the full path to the elements since the keys at depth "C" are the same:

public function recurseCounts($array,$dims = array(),$path = '') {
        foreach ($array as $key => $value) {
                $dims[] = ($path ? $Path.'_' : '').$key; // Add the full path (separated by '_')
                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,end($depth)); // pass it on
                }
        }
}
sirlancelot
+2  A: 

You are always adding to $dims.

The $dims will now go:

DimA1
DimA1, DimB1
DimA1, DimB1, DimC1
DimA1, DimB1, DimC2
DimA1, DimB1, DimB2
DimA1, DimB1, DimB2, DimC1
DimA1, DimB1, DimB2, DimC2

If you move the array_pop to outside the if you should be ok.

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
            } else {
                    $this->recurseCounts($value,$dims);
            }
            array_pop($dims);
    }

}

Peter Olsson