Hertus Hertus - 5 months ago 16
PHP Question

Parallel foreach loop

I have two foreach loops:

1st

foreach ($r['result']['achievements']['0']['achievements'] as $achievement){
if(!empty($achievement['rewardItems'])){
foreach ($achievement['rewardItems'] as $item){
echo "$item[name] $item[id]<br/>".PHP_EOL;
}
}
}


2nd

foreach ($r['result']['achievements']['0']['achievements'] as $item) echo '

<li class="span3 clearfix" data-tag=', $item['qualityId'], '>
<a
href="//de.wowhead.com/achievement=', $item['id'], '"
class="', $item['qualityId'], '"
>
<img
src="http://wow.zamimg.com/images/wow/icons/large/', $item['icon'], '.jpg"
alt="', htmlspecialchars($item['name']), '"
>
</a>
', $item['name'], '.
</li>';


They both are running for themselves. But I need them to run parallel, because I need the result from the 1st foreach loop into the result of the second loop.

Answer

I assume, you don't need parallel processing. You just need a second level loop. Try this code if it helps.

    foreach ($r['result']['achievements']['0']['achievements'] as $item) {
        echo '<li class="span3 clearfix" data-tag=', $item['qualityId'], '>';

        if(!empty($item['rewardItems'])){
            foreach ($item['rewardItems'] as $item1){
              echo "$item1[name] $item1[id]<br/>".PHP_EOL;
            }
        }

        echo '                           <a
                                            href="//de.wowhead.com/achievement=', $item['id'], '"
                                            class="', $item['qualityId'], '"
                                    >
                                            <img
                                                    src="http://wow.zamimg.com/images/wow/icons/large/', $item['icon'], '.jpg"
                                                    alt="', htmlspecialchars($item['name']), '"
                                            >
                                    </a>
    ', $item['name'], '.
                            </li>';
                        }

Please provide your current output and desired output. That will make us more clear about what you want to do.