Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:42:03+00:00 2026-05-27T08:42:03+00:00

I want to dynamically access value of variable, let’s say I have this array:

  • 0

I want to dynamically access value of variable, let’s say I have this array:

$aData = array(
  'test' => 123
);

Standard approach to print the test key value would be:

print $aData['test'];

However, if I have to work with string representation of variable (for dynamic purposes)

$sItem = '$aData[\'test\']';

how can I achieve to print aData key named test? Neither of examples provided below works

print $$sItem;
print eval($sItem);

What would be the solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:42:04+00:00Added an answer on May 27, 2026 at 8:42 am

    Your eval example is lacking the return value:

    print eval("return $sItem;");
    

    should do it:

    $aData['test'] = 'foo';
    
    $sItem = '$aData[\'test\']';
    
    print eval("return $sItem;"); # foo
    

    But it’s not recommended to use eval normally. You can go into hell’s kitchen with it because eval is evil.

    Instead just parse the string and return the value:

    $aData['test'] = 'foo';
    
    $sItem = '$aData[\'test\']';
    
    $r = sscanf($sItem, '$%[a-zA-Z][\'%[a-zA-Z]\']', $vName, $vKey);
    if ($r === 2)
    {
        $result = ${$vName}[$vKey];
    }
    else
    {
        $result = NULL;
    }
    
    print $result; # foo
    

    This can be done with some other form of regular expression as well.

    As your syntax is very close to PHP an actually a subset of it, there is some alternative you can do if you want to validate the input before using eval. The method is to check against PHP tokens and only allow a subset. This does not validate the string (e.g. syntax and if a variable is actually set) but makes it more strict:

    function validate_tokens($str, array $valid)
    {
        $vchk = array_flip($valid);
        $tokens = token_get_all(sprintf('<?php %s', $str));
        array_shift($tokens);
        foreach($tokens as $token)
            if (!isset($vchk[$token])) return false;
        return true;
    }
    

    You just give an array of valid tokens to that function. Those are the PHP tokens, in your case those are:

    T_LNUMBER (305) (probably)
    T_VARIABLE (309)
    T_CONSTANT_ENCAPSED_STRING (315)
    

    You then just can use it and it works with more complicated keys as well:

    $aData['test'] = 'foo';
    $aData['te\\\'[]st']['more'] = 'bar';
    
    $sItem = '$aData[\'test\']';
    $vValue = NULL;
    if (validate_tokens($sItem, array(309, 315, '[', ']')))
    {
        $vValue = eval("return $sItem;");
    }
    

    I used this in another answer of the question reliably convert string containing PHP array info to array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control and I want to dynamically insert a link to
I have a scenario where I want to dynamically add words of text to
I have a class with a property Value like this: public class MyClass {
I want to dynamically access a bunch of objects in my mxml. I can
I want to declare a function dynamically and I want to wrap any access
I want to be access some variables I've assigned dynamically from PHP in Smarty,
I want to access the Symfony2 entity methods dynamically by calling it's object. For
Basically I want to access a variable of a class, but I change the
Ok, I want to access div control by id witch is changed dynamically from
I want to dynamically hide/show some of the columns in a NSTableView, based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.