PHP Array嵌套到自身中

I am having an issue with my array.

My output displays as:

 <article id="tab1"> 
 <figure>
 <img src="http://cdn.resize.flexmls.com/ric/640x480/true/20140820133948567093000000-o.jpg" > 
<article id="tab2"> 
 <figure>
 <img src="http://cdn.resize.flexmls.com/ric/640x480/true/20140820133948862522000000-o.jpg" > 

I can't get the tag to close...

Here is my code:

<?php
$photos = $rets->GetObject("Property", "640x480", $row["field_Value"], "*", 1);
foreach ($photos as $photo) {
    $listing = $photo['Content-ID'];
    $number = $photo['Object-ID'];
    $location = $photo['Location'];

    if ($photo['Success'] == true) {
        // display on page
        echo "<article id=\"tab$number\"> 
 <figure>
 <img src=\"{$photo['Location']}\" > 
";
    } else {
        echo "({$listing}-{$number}): {$photo['ReplyCode']} = {$photo['ReplyText']}
";
    }
}
?>   

<article id="tab<?=$number?>">
    <figure>
        <img src="<?=$location?>" alt="Photo" width="586" height="402" />
    </figure>

How come you can't just close it in the echo part like you have for the beginning of it?

<?php
    $photos = $rets->GetObject("Property", "640x480", $row["field_Value"], "*", 1);
    foreach ($photos as $photo) {
            $listing    =   $photo['Content-ID'];
            $number     =   $photo['Object-ID'];
            $location   =   $photo['Location'];

            if ($photo['Success'] == true) { ?>
                <article id="tab<?php echo $number; ?>">
                    <figure>
                        <img src="<?php echo "{$photo['Location']}"; ?>" >
                    </figure>
                </article>
                <?php 
                }
            else 
                echo "({$listing}-{$number}): {$photo['ReplyCode']} = {$photo['ReplyText']}
";
        } ?>   

If I fully understand what your intent is, you just need to close the tags in that initial if statement. So the code would look like this

<?php
$photos = $rets->GetObject("Property", "640x480", $row["field_Value"], "*", 1);
foreach ($photos as $photo) {
    $listing = $photo['Content-ID'];
    $number = $photo['Object-ID'];
    $location = $photo['Location'];

    if ($photo['Success'] == true) {
        // display on page
        echo "<article id=\"tab$number\"> 
 <figure>
 <img src=\"{$photo['Location']}\" > 
";
        echo "</figure> 
";
        echo "</article> 
";
    } else {
        echo "({$listing}-{$number}): {$photo['ReplyCode']} = {$photo['ReplyText']}
";
    }
?>