1

I have a lot of code but basically its a foreach loop with values collected and $i is the dynamic number.

I want to get a bunch of attachments from a json and have following inside the foreach

<?php

$i=0;
foreach($attribs->contentblocks->add_to_content as $count) { 

if($attribs->contentblocks->add_to_content[$i]=='3') { 

// other code goes here

?>

<?php for($a = 1; $a < 30; ++$a){ ?>

<?php echo $attribs->contentblocks->blockfile[$a][$i];?>

<?php } ?>

<?php $i++; }} ?>

But... it wont work. If i replace [$a] with an actual number like 1 or 2 or 3 it works.

How do i add the dynamic $a number into that line to work with my $i as well?

Cheers John

13
  • 1
    what do you mean foreach loop? thats a for loop, and on our side, there is nothing wrong with it Commented Sep 28, 2014 at 5:44
  • I have a foreach loop and want the coiunter loop to work inside it - 29times Commented Sep 28, 2014 at 5:45
  • did you try to var_dump($a); Commented Sep 28, 2014 at 5:46
  • You want $a as incremented number or $i Commented Sep 28, 2014 at 5:50
  • @AdamSinclair the var dump returns int(1) int(2) int(3) int(4) etc... Commented Sep 28, 2014 at 5:51

2 Answers 2

1

Then kindly try this one if you hav said in the comments, blockfile1, blockfile2, ... etc.:

<?php echo $attribs->contentblocks->{'blockfile'.$a}[$i]; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Now i can finish this and have a coconut juice with ice in 120kmh winds. :)
0

If you want $i as incremented variable. You can achieve this by doing:

<?php
$i = 0;
 for($a = 1; $a < 30; ++$a){ ?>

<?php echo $attribs->contentblocks->blockfile[$a][$i];?>

<?php $i++; } ?>

1 Comment

$i is already set as 0 then 1 then 2 - depends on how many blocks there are. The $a is the one that i need to work as it is a loop inside the foreach for $i

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.