I need to create an array dynamically from a string value. Please advise how is it possible to retrieve the array values by keys.

$mystring = 'myarray';
    $myarray = ['a','b','c'];
    echo '<pre>';
    print_r($$mystring); // it works properly
    echo '</pre>';
    echo $$mystring[0]; // returns error

Recommended Answers

All 2 Replies

Change last line to:

echo ${$mystring}[0];

And it will work, otherwise it will refer to the name of the string myarray.


Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.