0

what is the correct syntax to use isset with a dynamic variable name and dynamic key name, without writing the keyname in braces.

Example:

$ab[0] = 'test';
$var1="ab";
$var2="[0]";
$var3="0";

//This works
if (isset(${$var1}[0])){
   echo "success";
}

//This works too
if (isset(${$var1}[$var3])){
   echo "success";
}


//But this doesn't.
if (isset(${$var1}$var2)){
   echo "success";
}

What can I do that the 3rd example works? I can't use the first or second example, because I dont't know how many subarrays are inside the array.

4
  • Possible duplicate of PHP Variable Variables with array key Commented Oct 19, 2018 at 17:22
  • 2
    This might be an example of an XY problem (meta.stackexchange.com/q/66377/159630). Could you back up a few steps and explain your goal to us a bit more? Commented Oct 19, 2018 at 17:22
  • seems like you should use other functions instead of this. Things like is_array to determine what to do, instead of trying to see if it is an array by variable varibles Commented Oct 19, 2018 at 17:25
  • do something as stackoverflow.com/questions/9628176/… Commented Oct 19, 2018 at 17:27

1 Answer 1

0

You cannot type brackets to variable.

Here is code:

<?php
$ab[0] = 'test';
$var1="ab";
$var2="0";
$var3="0";

//This works
if (isset(${$var1}[0])){
   echo "success";
}

//This works too
if (isset(${$var1}[$var3])){
   echo "success";
}


//This works too
if (isset(${$var1}[$var2])){
   echo "success";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.