I have been working on resolving this problem for two days. I'm sure it's a simple fix but I am fairly new to PHP. I need to generate three items using a loop that cycles through an array. I also need to append a number to an ID AND I need to apply a selected class based on parameters identified elsewhere. 

Another user was kind enough to help me along this far and I've almost accomplished what I need. The problem is I have two loops that create more instances than I need and I'm not sure how to keep my counter while getting rid of the loop. 

What I have so far:

```
<?php 
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
$pages = array();
foreach ($tabs as $tab){
    $pageid = opt($tab);
    $post = get_post($pageid);
    $content = apply_filters('the_content', $post->post_content);
    $icon = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
    $pages[] = array('pageid' => $pageid, 'post' => $post, 'content' => $content, 'icon' => $icon);
}

$tab_position = opt('tab_position');
if ($tab_position == '' || count($tab_position) != 3) {
    $tab_position = array(0, 1, 2);
}

for($i=0; $i < count($tab_position); $i++){
    $selected = '';
    if (opt('default_selected_tab') == $tab_position[$i]){
        $selected = 'class="selected"';
    }

    foreach ($pages as $page){
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $page['content']; ?>
</a>
<?php
    }
}
?>

```

That creates this:

```
<a class="selected" href="#tab0">
    <p>1st Content</p>
</a>
<a class="selected" href="#tab0">
    <p>2nd Content</p>
</a>
<a class="selected" href="#tab0">
    <p>3rd Content</p>
</a>
<a href="#tab1">
    <p>1st Content</p>
</a>
<a href="#tab1">
    <p>2nd Content</p>
</a>
<a href="#tab1">
    <p>3rd Content</p>
</a>
<a href="#tab2">
    <p>1st Content</p>
</a>
<a href="#tab2">
    <p>2nd Content</p>
</a>
<a href="#tab2">
    <p>3rd Content</p>
</a>

```

**I need this:**

```
<a class="selected" href="#tab0">
    <p>1st Content</p>
</a>
<a href="#tab1">
    <p>2nd Content</p>
</a>
<a href="#tab2">
    <p>3rd Content</p>
</a>

```