1
votes

I have the follow array structure and I am successfully looping through the [LoyaltyHistory] part using a for each loop:

Array
(
    [0] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 5
                    [user_id] => 32
                    [points] => 4
                    [date] => 2012-02-07
                    [total] => 146
                )

            [User] => Array
                (
                )

        )

    [1] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 6
                    [user_id] => 32
                    [points] => -6
                    [date] => 2012-02-07
                    [total] => 140
                )

            [User] => Array
                (
                )

        )

    [2] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 7
                    [user_id] => 32
                    [points] => -5
                    [date] => 2012-02-07
                    [total] => 135
                )

            [User] => Array
                (
                )

        )

    [3] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 8
                    [user_id] => 32
                    [points] => 15
                    [date] => 2012-02-07
                    [total] => 150
                )

            [User] => Array
                (
                )

        )

    [4] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 9
                    [user_id] => 32
                    [points] => 5
                    [date] => 2012-02-10
                    [total] => 155
                )

            [User] => Array
                (
                )

        )

    [5] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 10
                    [user_id] => 32
                    [points] => 155
                    [date] => 2012-03-04
                    [total] => 305
                )

            [User] => Array
                (
                )

        )

    [6] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 11
                    [user_id] => 32
                    [points] => -5
                    [date] => 2012-03-04
                    [total] => 300
                )

            [User] => Array
                (
                )

        )

    [7] => Array
        (
            [LoyaltyHistory] => Array
                (
                    [id] => 12
                    [user_id] => 32
                    [points] => 5
                    [date] => 2012-03-05
                    [total] => 305
                )

            [User] => Array
                (
                )

        )

)

the for each loop is structured as follows:

<?php foreach ($history as $hist) 
    {
       if($hist['LoyaltyHistory']['points'] > 0) $hist['LoyaltyHistory']['points'] = '+'.$hist['LoyaltyHistory']['points'];?>
                                <tr>
                                    <td><?php echo $hist['LoyaltyHistory']['date']; ?></td>
                                    <td><?php echo $hist['LoyaltyHistory']['points']; ?></td>
                                    <td><?php echo $hist['LoyaltyHistory']['total']; ?></td>
                                </tr>
                    <?php   } ?>

The for each loop is outputing the correct elements from the array but it is causing the following warning: 'Warning (2): Invalid argument supplied for foreach()' and I can't figure out why.

1
Could it be because you're using $history as your looping variable but call $h inside of the loop? - user554546
Well spotted, forgot to update that reference from $h to $hist, will make an amendment to original post. Unfortunately that is not what is causing the issue. - Cronin O'M
I tried you example with your data and it works correctly. Try to define after which cycle you get this warning - a.tereschenkov

1 Answers

4
votes

This error occurs mainly when the argument received by foreach is not an array. Try adding var_dump($history) just before. One of your record must have a null value instead of an empty array.