jonnypixel
jonnypixel

Reputation: 327

php foreach with counter loop within it

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

Upvotes: 1

Views: 50

Answers (2)

Kevin
Kevin

Reputation: 41885

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

<?php echo $attribs->contentblocks->{'blockfile'.$a}[$i]; ?>

Upvotes: 1

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

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++; } ?>

Upvotes: 0

Related Questions