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.

Vidarrus
  • 175
  • 4
  • 18
  • Possible duplicate of [PHP Variable Variables with array key](https://stackoverflow.com/questions/10466815/php-variable-variables-with-array-key) – scrowler Oct 19 '18 at 17:22
  • 2
    This might be an example of an XY problem (https://meta.stackexchange.com/q/66377/159630). Could you back up a few steps and explain your goal to us a bit more? – waterloomatt Oct 19 '18 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 – Ice76 Oct 19 '18 at 17:25
  • do something as https://stackoverflow.com/questions/9628176/using-a-string-path-to-set-nested-array-data – splash58 Oct 19 '18 at 17:27

1 Answers1

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";
}
?>
Marty1452
  • 430
  • 5
  • 19