2197 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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
  • Has been part of the Envato Community for over 4 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
iamthwee says

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

2197 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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
  • Has been part of the Envato Community for over 4 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
iamthwee says

What?

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

3072 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Had an item featured on Envato Market
+5 more
wickedpixel says

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

812 posts
  • Has been part of the Envato Community for over 4 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
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…

2197 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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
  • Has been part of the Envato Community for over 4 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
iamthwee says

Again we’re going to need a better example…

3072 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Had an item featured on Envato Market
+5 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..

2197 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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