0

I have the following for loop:

<?php foreach($this->relatedItems as $key=>$item): ?>
<div class="column1"><?php echo $item->extraFields->field1->value; ?></div>
<div class="column2"><?php echo $item->extraFields->field2->value; ?></div>
<div class="column3"><?php echo $item->extraFields->field3->value; ?></div>
<?php endforeach; ?>

Id like to create a variable for each custom field for each related item passed, for use outside of the loop.

Ideally I would have variables:

relatedItems1_field1, relatedItems1_field2, relatedItems1_field3, relatedItems1_field4, relatedItems2_field1, relatedItems2_field2, relatedItems2_field3, relatedItems2_field4, relatedItems3_field1, relatedItems3_field2, relatedItems3_field3, relatedItems3_field4

Any assistance would be much appreciated, many thanks in advance!

8
  • Don't you already have those values in the $this->relatedItems list? Something like: $this->relatedItems[0]->extraFields->field1->value ... etc? Commented Jun 9, 2016 at 5:44
  • The output printed in each new line is inside the loop? The values will end with 4 each time? Commented Jun 9, 2016 at 5:51
  • Hi Magnus, Yes but they end when the loop go's again. Hi Megan, Yes this output is inside the loop but I also need to use the variables outside the loop. The relateditems and field values will go upto 10. Commented Jun 9, 2016 at 5:53
  • Ending with 4 ? relatedItems3_field4 , relatedItems2_field4 Commented Jun 9, 2016 at 5:54
  • Upto 10, (sorry i posted previous comment before I had finished typing it and had to edit) Commented Jun 9, 2016 at 5:56

2 Answers 2

1

Alternative 1

As long as $this->relatedItems isn't an associative array (having keys that aren't in sequence or strings as key), then you can use this outside the loop:

$this->relatedItems[0]->extraFields->field1->value

Alternative 2

If the array keys in the $this->relatedItems are unpredictable/associative, you can use this code:

<?php 
$keys = [];
foreach($this->relatedItems as $key => $item): 
    $keys[] = $key;
?>

    <div class="column1"><?php echo $item->extraFields->field1->value; ?></div>
    <div class="column2"><?php echo $item->extraFields->field2->value; ?></div>
    <div class="column3"><?php echo $item->extraFields->field3->value; ?></div>

<?php 
endforeach; 
?>

This will create an array with all the keys (0-9, if you have 10 array items in the $this->relatedItems).

Then you can use this outside the loop:

$this->relatedItems[$keys[0]]->extraFields->field1->value

No need to copy all the values to new variables, since you already have them in your scope.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use multi dimensional arrays for storing values

Check out the example code.

<!DOCTYPE html>
<html>
  <body>

    <?php
      $cars = array
  (
       array("Volvo",22,18),
       array("BMW",15,13),
       array("Saab",5,2),
       array("Land Rover",17,15)
  )    ;

 echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
 echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
 echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
 echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>

</body>
</html>

Like this

$items = array();
foreach($group_membership as $username) {
 $items[] = $username;

}

print_r($items);

1 Comment

Many thanks, although im wondering how I apply this to the example i gave? Any further help would be so much appreciated! Thanks again!

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.