-1

Is it possible to read array element using variable? I'd like to set $vid in one place depending on the configuration, and then use multiple times i.e. $detailrow["customfields1"]; I want to do this:

$vid = 1;
$detailrow["customfields$vid"];

But no response.

Tried:

$detailrow["customfields{$vid}"];
$detailrow['customfields'.$vid];

but result is the same.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
jakubplus
  • 307
  • 4
  • 17

2 Answers2

1

Of course you can do this:

$tmp=array("name" => "foo", "bar" => "name", "field1" => "value1");

You could then do sth. like

echo $tmp["name"];

will print 'foo'

echo $tmp[$tmp["bar"]];

will also print 'foo'

Or

$i=1;
echo $tmp["field".$i]

will print 'value1'

cb0
  • 8,415
  • 9
  • 52
  • 80
0

i have tested your code and its working

<?php
 $vid = 1;
 $detailrow["customfields1"]="rajeev";
 echo $detailrow["customfields$vid"];

?>
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41