aborted
aborted

Reputation: 4541

Foreach inside for loop complication

I'm having trouble with a loop inside another one. I don't know how to handle this situation and I don't get the expected result - I get my expected result but multiplied.

This is my code:

<div id="view">
    <?php 
    for ($i = 0; $i < sizeof($user_roll_array); $i++): 
        $roll_data =& $user_roll_array[$i];

        $roll_date = $roll_data['time'];
        $roll_ids  = $roll_data['image_ids'];
        $roll_ids  = explode('|', $roll_ids);
        $roll_key  = $roll_data['key'];

        foreach ($roll_ids as $image_id):
            $image_name = $image->get_name($image_id);

    ?>
    <div class="roll-spot">
        <?php echo $image_name; ?>
    </div>
    <?php endforeach; endfor; ?>
</div>

This is what $user_roll_array contains:

Array
(
    [0] => Array
        (
            [id] => 1
            [user_id] => 1
            [image_ids] => 10|9
            [time] => 1359244752
            [key] => 8O0F5k8G9Y1H4b7
        )

    [1] => Array
        (
            [id] => 2
            [user_id] => 1
            [image_ids] => 13|12|11|10|9
            [time] => 1359245133
            [key] => n9G7v49E2Q5h0j7
        )

    [2] => Array
        (
            [id] => 3
            [user_id] => 1
            [image_ids] => 13|12
            [time] => 1359285360
            [key] => 2Q0t1Z3S2r7n5f9
        )

    [3] => Array
        (
            [id] => 4
            [user_id] => 1
            [image_ids] => 10|9
            [time] => 1359285377
            [key] => 4L6w6R2r2Q0c1g9
        )

    [4] => Array
        (
            [id] => 7
            [user_id] => 1
            [image_ids] => 10|9
            [time] => 1359288800
            [key] => 4t1X9P8l9H7C1F6
        )

)

Based on the array I create inside the for loop, I need to go through each element and get its name via a method ($image->get_name($id)). Then I need to use these names below.

I expect 5 rows to be returned, but I get 13 rows, and the names are duplicated a few times.

If someone could explain how to fix this, I'd understand the issue and prevent future similar problems to occur.

Thanks a lot.

Upvotes: 1

Views: 7046

Answers (2)

hohner
hohner

Reputation: 11588

The reason you are getting 13 results is because although there are 5 top-level iterations, there are 13 sub-iterations (the IDs) which you are looping through.

If you want just the 5 iterations to happen, you shouldn't be doing the sub-loop within them. This is the part of your code which is confusing you:

<? foreach ($roll_ids as $image_id): ?>

   <? $image_name = $image->get_name($image_id); ?>
   <div class="roll-spot">
       <?php echo $image_name; ?>
   </div>

<?php endforeach;?>

This foreach is itself inside the main loop, and echoing out a div for each ID has access to. In the life cycle of the main array, this will happen 13 times. The reason you are seeing duplicates is because you have top-level array elements which contain the same IDs as other elements.

Upvotes: 2

Wezy
Wezy

Reputation: 667

remove the ampersand in

$roll_data =& $user_roll_array[$i];

Upvotes: 0

Related Questions