Looping arrays keys

Hi I’m doing a listing page for drupal and have 5 of these arrays under $node.

field_data_sheet_link_1
field_data_sheet_link_2
field_data_sheet_link_3
field_data_sheet_link_4
field_data_sheet_link_5

The value I want to get is in:

$node->field_data_sheet_link_1[0][‘view’]
$node->field_data_sheet_link_2[0][‘view’]
$node->field_data_sheet_link_3[0][‘view’]
$node->field_data_sheet_link_4[0][‘view’]
$node->field_data_sheet_link_5[0][‘view’]

How do I do this with loops instead of repeating the statements??
PS: Title should be looping array names.


for($i = 1; $i <=5; $i++)
{
  $var = 'field_data_sheet_link'.$i;
  echo $node->{$var}[0]['view'];
}

or


for($i = 1; $i <=5; $i++)
{
  echo $node->${'field_data_sheet_link'.$i}[0]['view'];
}

Whichever you like best :slight_smile:

cool…u made it so simple. i had a close one to your first solution. seems to be the same to me. but why doesn’t it work?


$var1 = 'field_data_sheet_link_' . $i. '[0]["view"]';
echo $node->{$var1};

Because that assumes $node has a property e.g. ‘field_data_sheet_link_0[0][“view”]’, which it doesn’t (and is an invalid variable name to booth).
It has a property “field_data_sheet_link”, which is an array, that has key “0”, which also is an array that has a key “view”.

ic…tink i got it. thanks! this is certainly useful :slight_smile: