开发者

PHP, pass string containing array keys to find array value

开发者 https://www.devze.com 2023-03-23 04:23 出处:网络
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

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 o开发者_运维知识库f 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消