0

Pulling my hair out with this one, please help

I have an array $address

$access=sprintf("['results'][1]['address_components'][1]['long_name']");

I want to be able be able get data from array using the string i.e

$home=$address[$access]

Still having probs

print_r($address['results'][1]['address_components'][1]);

$key=sprintf("[results][1][address_components][1][long_name]");

printf("key=%s\n", $key);

$home = eval($address . $key);

exit;

--- Returns

Array
(
    [long_name] => High St
    [short_name] => A4151
    [types] => Array
        (
            [0] => route
        )

)


key=[results][1][address_components][1][long_name]

PHP Parse error:  syntax error, unexpected '[', expecting '(' in /media/www.h.com.dev/postCode/post.php(72) : eval()'d code on line 1
3
  • Is there a reason why you have to sprintf that string? Commented Jun 26, 2011 at 17:57
  • print_r($access) to get some more information and output that information over here also?? Commented Jun 26, 2011 at 17:59
  • sprintf, Just old programming from my C days Commented Jun 26, 2011 at 18:05

2 Answers 2

1

If you don't have any user-provided input in $access, you can safely use eval()...

$home = eval("return \$address{$access}");

Note that when doing an eval(), you are passing code as a string. Therefore, you need to ensure that $address is passed as an actual string, not as a variable (use single quotes ' or escape the dollar sign \$ in the double-quoted string); as for $access, you want that to be parsed as code so simply concatenate it.


If you do have user-provided input, you have to parse $access. You can parse $access using token_get_all().

function array_get_node($array, $nodePath) {
  $nodePath = '<?php ' . $nodePath;

  $tokens = token_get_all($nodePath);
  array_shift($tokens);

  $current = $array;
  $moved = false;

  var_dump($tokens);

  $tokCount = count($tokens);
  for($i = 0; $i < $tokCount; $i++) {
    if($tokens[$i] === '[' && isset($tokens[$i+2])
       && $tokens[$i+2] === ']' && is_array($tokens[$i+1])) {

      $node = null;
      switch($tokens[$i+1][0]) {
        case T_LNUMBER:
          $node = (int) $tokens[$i+1][1];
          break;

        case T_CONSTANT_ENCAPSED_STRING:
          $node = preg_replace('#^[\'"](.*)[\'"]$#', '\1', $tokens[$i+1][1]);
          break;

        case T_STRING:
          $node = $tokens[$i+1][1];
          break;            

        default:
          return null;
          break;
      }

      if(!isset($current[$node])) return null;

      $current = &$current[$node];
      $moved = true;
      $i+=2;
    }
  }

  if($moved)
    return $current;

  return null;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you trust $access you could use eval. If not, you have to parse $access...

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.