3

long time reader, first time question-er.

I've found myself in a unique situation, and the most effective way I can find to solve my problem is by building a string of keys to obtain a value. I was having trouble with the code block indenting on here, so I've provided my code snippet on Pastebin: http://pastebin.com/Nn8xS5Vx

What I'm trying to do there is step through each level of the structure, checking each level for errors, and if any are found, reporting them back into the $errors_array in the following format:

[Level eg:Enterprise]
  [0] Enterprise Name
[Platform]
  [0] Platform Name

The problem is that I'm not going down through the levels when I retrieve the name, it's looking at the root of the array. My idea was to write a for loop to construct a string to provide the current level, so the name can be retrieved.

What I'm looking to do is build a string containing keys of each level, and then use that string of keys to refer to the $node variable and pull the name respectively. Here's a pseudo-example:

$keyString = "[Enterprise][Platform][Offering]"

And I would then use something to the effect of:

$node[$keystring]['name']

I hope I've explained that well enough. If anyone has any suggestions as to how I could achieve this, or even a better method of getting the same end result, I would greatly appreciate it.

2 Answers 2

1

If I've understood your problem correctly, you want to get a value in a nested array by a list of keys. There are a few ways to achieve this. Here's a couple:

// setup
$data = array(
    'foo' => array(
        'bar' => array(
            'baz' => array(
                'quux' => 'value'
            )
        )
    )
);

$search = array('foo', 'bar', 'baz', 'quux');

// Method 1 - search by reference:
$value = $data;

foreach ($search as $key) {
    if (is_array($value) && array_key_exists($key, $value)) {
        $value = &$value[$key]; // use the reference to chase down the array
    } else {
        // handle errors here
    }
}

var_dump($value); // 'value'


// Method 2 - search by recursion:
function getValue($value, array $keys) {
    if ($keys === array()) {
        return $value; // got to the end of the list of keys
    }

    $key = array_shift($keys);

    if (is_array($value) && array_key_exists($key, $value)) {
        return getValue($value[$key], $keys);
    }

    // handle errors here
}

var_dump(getValue($data, $search)); // 'value'

I think this slightly simplifies your problem, but I hope it helps.

1

Have you thought about using array_walk_recursive instead? I'll give you the current key, and you could easily enough use a global variable (maybe dirty and bad, but it should work) to use as a stack to keep track of the array keys above the current value.

2
  • array_walk_recursive takes a $userdata parameter, so you could avoid the global I guess, but I think you'd run into problems as the callback only gets passed the non-array values, so you wouldn't be able to track your position in the nested array. Commented Jul 27, 2011 at 22:18
  • Hmm...yes, I see what you mean. Use array_walk then, and inside the callback call array_walk on any arrays that are detected? Do this ad infinitum and you recurse through the entire array structure. Commented Jul 27, 2011 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.