Hello.

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

Change last line to:

echo ${$mystring}[0];

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

Jump to Post

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.

@cereal

Thank you!

Be a part of the DaniWeb community

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