0

Here I have code such as following.

$Var =  "Gas";
$Gasname[0] ="abc";
$Gasno[1] = 123;

echo ${$Var."name"[0]};

I have condition on $Var if value is Gas I will user Gase array in else case have to use another array like wise there are dynamic types of array.

Is there a way I can use dynamic array variable name.

1
  • 1
    This is a very bad idea. Use array with keys: array('gas' => [], 'another_gas' => []') Commented Apr 13, 2022 at 14:48

2 Answers 2

1

You were nearly there, the array index is not part of the variable name.

echo ${$Var."name"}[0];
Sign up to request clarification or add additional context in comments.

Comments

1

Move [0] out of ${} part, as it's not part of dynamic name:

$Var =  "Gas";
$Gasname[0] ="abc";
$Gasno[1] = 123;

echo ${$Var."name"}[0];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.