1

So I have arrays of different lengths

pathes= array(array('f1e9'),
              array('c0d9', '0', 'form_values', '6e13')
);

For each path I would like to use each value as an index to scan a separate array.

foreach ($pathes as $key => $val){

    $new_path = '$array_to_search';

    foreach ($val as $index){
       $newpath .= '[' . $index . ']';
    }
}

So within the loop the $new_path variable would be a string that looked like:

$new_path = '$array_to_search['f1e9']'

and

$new_path = '$array_to_search['c0d9']['0']['form_values']['6e13']'

But then I would have to be able to evaluate this string, and I do not know how to do this.

I think the answer might lie somewhere in variable variables, but I am not sure how to go about this.

Sincere thanks for any help. It is greatly appreciated!

ambe5960
  • 1,870
  • 2
  • 19
  • 47

1 Answers1

0

I think the following recursive function would be the solution here:

function getByPath($value, array $path)
{
  if (empty($path)) return $value;
  if (!is_array($value)) return;
  if (array_key_exists($path[0], $value)) return getByPath($value[$path[0]], array_slice($path, 1));
}
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • thanks! I have to add a lot to it as what I'm doing has multiple parts, but this should definitely put me down the right path. Much appreciated. – ambe5960 Jul 24 '15 at 21:21