2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
iamthwee says

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

2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
iamthwee says

What?

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

3072 posts
  • Has been a member for 6-7 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Exclusive Author
  • Romania
wickedpixel says

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

812 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
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…

2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
iamthwee says

Again we’re going to need a better example…

3072 posts
  • Has been a member for 6-7 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Exclusive Author
  • Romania
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..

2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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