0

I have an array structure of 60 elements. I'd like to use a for/foreach/while to read this structure.

This is what I have :

$this->details->field_link_01[0]['title']
$this->details->field_link_02[0]['title']
..
$this->details->field_link_60[0]['title']

And what i need is the following.

$myvar = eval ( "$this->details->field_link_" . $cont . "[0]['title']" )

What I have seen is PHP let to use $ as evaluation function

$myvar = ${"this->details->field_link_" . $cont . "[0]['title']" }

But it didn't work.

Is there any other solution ? Which PHP version ? 5.2 , 5.6 , 7 ?

1
  • try $this->details->field_link_{$count}[0]['title'] Commented Mar 7, 2017 at 13:39

1 Answer 1

1

Have a look at Variable variables and sprintf.

for ($i = 1; $i <= 60; $i++) {
    $fieldName = sprintf("field_link_%02d", $i);
    $fieldLink = $this->details->$fieldName;

    $myvar = $fieldLink[0]['title'];

    echo $myvar;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.