1

Given the following array of key names in the following format (number of fields can change):

$field_names = Array
(
[0] => web
[1] => results
[2] => result
[3] => title
)

I'd like to access the 'content' key value of the following multi-leveled array:

$values=stdClass Object(
  [web] => Array(
    [results] => Array(
      [result] => Array(
        [title] => Array(
          [type] => default
          [content] => Sample content
        )
      )
    )
  )
)

Sample example how value can be accessed given above arrays:

$value = $values->{$field_names[0]}[$field_names[1]][$field_names[2]][$field_names[3]]['content'];

For the sake of simplicity (keep it plain PHP), I won't get into much details as the existing code is part of some handler which is part of some plugin which is part of some module which is part of another module which is part of content management system in order to parse YQL results, but its logic is broken.

The code looks like:

$field = array_shift($field_names);
$value = $values->$field;
foreach ($field_names as $field) {
  if (is_array($value)) {
    $value = $value[$field];
  }
}

I've tried to do a dirty patch like:

$value = is_string($value[$field]) ? $value[$field] : $value[$field]['content'];

And it worked for one example, but it doesn't work for all cases, like one above.

I'm aware of recursive functions and array_walk_recursive(), but I'd like to avoid headache of using them in order to make it simple as possible.

Is there any simple way of accessing value of multi-leveled array having dynamic array of key names?

1

2 Answers 2

2

Recursive solutions can also be simple

Your data:

$field_names = Array("web", "results","result","title");
$values->web["results"]["result"]["title"]=
            Array("type"=> "default", "content"=>"Sample content");

The function:

function getField($obj, $keys) {
    return ($key=array_shift($keys)) ? getField($obj[$key], $keys) : $obj["content"];
}

echo getField((Array)$values, $field_names);

But if you want a not recursive one, here it is:

$obj=(Array)$values;
while ($key=array_shift($field_names)) $obj=$obj[$key];
echo $obj['content'];
Sign up to request clarification or add additional context in comments.

3 Comments

Tested recursive one-liner, really simple and works great, thanks. Posted patch here.
Look at that @Adam . You just helped a commit to Drupal!
If they don't give my name to the next version, I don't understand. ;-)
1

Here's a simple way, not recursive using a reference (taken from How to write getter/setter to access multi-level array by key names?):

function get($object, $path) {
    $prop = array_shift($path);
    $temp =& $object->$prop;

    foreach($path as $key) {
        $temp =& $temp[$key];
    }
    return $temp;
}

$value = get($values, $field_names)['content']; //PHP 5.4.0

Comments

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.