Question

This is a question related to getting Drupal CCK fields (just in case that happens to change anything).

I have several Drupal CCK fields with similar names. They have the same name with a number at the end. that I'd like to pull values from these fields (ten fields total). This is the syntax for accessing the fields values:

$node->cck_field_1[0]['value']
$node->cck_field_2[0]['value']
$node->cck_field_3[0]['value']

…etc.

Since they're all separate fields, but they're numbered, I'd like to just loop through incrementally to write out what I need (there's a lot more to what I'm writing than just accessing these fields' data, but they're the determining factors of the rest), but I can't figure out how to insert a variable into that part of the code.

e.g., (if $i were the incremental number variable), I'd like to be able to write the following string as a variable:

'$node->cck_field_' . $i . '[0]["value"]'

I understand about using the curly brackets to create a variable name from a string, but the part I need the variable in needs to be outside of the string. e.g. this works:

${node}->cck_field_1[0]['value']

but this doesn't:

${node->cck_field_1}[0]['value']

(so I can't write ${'node->cck_field'.$i}[0]['value'] )

So how can write this so that I can use $i in place of the number?

Was it helpful?

Solution

This should work:

$node->{'cck_field_' . $i}[0]['value']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top