/    Sign up×
Community /Pin to ProfileBookmark

pleas help: dynamic access to multiarray element

Hello

First time here, noob in php, hope to learn ?

$foo = “bar”;
$myArray[$foo] = array();

This will work.

$foo = “bar”;
$anotherFoo = “blah”;
$myArray[$foo][$anotherFoo] = array ();

This also.

$myArray[$key1][$key2][$key3][… and so on] = array ();

So everythin following this pattern also will work.

Now assume I have a multidimensional associative array.
How to store each key of the path as one variable ie:
$foo = “$key1][$key2][$key3”;
$myArray[$foo] = array ();

I managed to achieve this by using eval() but I know that is a bad way to go:
$foo = “$key1][$key2][$key3”;
$execute = ‘$myArray[‘.$foo.’] = array ();’;
eval ($execute);

Question is: how to achieve same thing without eval () ?
I want to acces a multiarray by ‘array key paths’ stored in variables, when I don’t know how deep I will go (so I can’t use a pattern).

I will be very grateful for any help.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @desidnova spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...