2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says

Hi guys, I want to store the array ‘path’ in the variable so I can generate it dynamically but I am having trouble with using it later… here is an example:

$my_path = '[something][value]';

echo $my_array{$my_path};

Any idea how to make it work?

812 posts
  • 5 Years of Membership
  • Collector Level 2
  • Community Superstar
  • Exclusive Author
iamthwee
says

You need to reference the element in the array as it is a multidimensional array.

2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says

LOL :D I know, the $my_array has both something and value fields thats not the problem, this is just an example… the question is how to reference element in an array when you have path in variable.

812 posts
  • 5 Years of Membership
  • Collector Level 2
  • Community Superstar
  • Exclusive Author
iamthwee
says

What?

You’ll have to elaborate, please give a more extensive example.

3072 posts
  • Affiliate Level 3
  • Author Level 6
  • Collector Level 2
  • Weekly Top Seller
+7 more
wickedpixel
says

maybe is too advanced for me… but what is with the curly braces?

812 posts
  • 5 Years of Membership
  • Collector Level 2
  • Community Superstar
  • Exclusive Author
iamthwee
says

maybe is too advanced for me… but what is with the curly braces?

It’s essentially wrong, you would only use curly braces around clauses such as if/while or defining functions and classes.

My guess is the OP made a typo…

2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says

I find the example of using the curly braces somewhere on the web, I don’t want to use them I am just trying to figure a way to make it work and I know that the curly braces are wrong, so I need another solution :)

812 posts
  • 5 Years of Membership
  • Collector Level 2
  • Community Superstar
  • Exclusive Author
iamthwee
says

Again we’re going to need a better example…

3072 posts
  • Affiliate Level 3
  • Author Level 6
  • Collector Level 2
  • Weekly Top Seller
+7 more
wickedpixel
says
so… you generate a $my_path from two strings?
$my_path = $first_string.$second_string;
then use that so search for a value in an array?
$value = $my_array[$my_path];

being this dynamic probably is better if you search first if the array has that key in it ?

if (array_key_exists($my_path, $my_array)) {
$value =  $my_array[$my_path];
}

or else you will get errors..

2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says

Hmm but wont I get path like this:

$my_array[[something][value]]

// and what I really need is 

$my_array[something][value]

// or is it the same in php?
by
by
by
by
by
by