# Cakephp3- foreach loop iteration failed

**URL:** https://discourse.cakephp.org/t/cakephp3-foreach-loop-iteration-failed/2705
**Category:** Need Help
**Created:** [June 13, 2017, 6:46am UTC](https://discourse.cakephp.org/t/cakephp3-foreach-loop-iteration-failed/2705 "2017-06-13T06:46:09Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![themarsbeing](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@themarsbeing](https://discourse.cakephp.org/u/themarsbeing)
#### Post date: [June 13, 2017, 6:46am UTC](https://discourse.cakephp.org/t/cakephp3-foreach-loop-iteration-failed/2705/1 "2017-06-13T06:46:09Z")

</div>

I’m fetching a list of users as an array and paginate the data and show it in the view as a table.

To iterate the array, I use foreach loop. But my foreach loop iteration is not working.

Here’s a sample array when I did print\_r() I have id, email and full\_name fields which I wanna display in view.

```
Cake\ORM\ResultSet Object
(
    [items] => Array
        (
            [0] => App\Model\Entity\Role Object
                (
                    [_matchingData] => Array
                        (
                            [Users] => App\Model\Entity\User Object
                                (
                                    [id] => 1
                                    [email] => johndoe@doe.com
                                    [full_name] => John Doe
                                    [[new]] => 
                                    [[accessible]] => Array
                                        ( [*] => 1 )
                                    [[dirty]] => Array
                                        ( )
                                    [[original]] => Array
                                        ( )
                                    [[virtual]] => Array
                                        ( )
                                    [[errors]] => Array
                                        ( )
                                    [[invalid]] => Array
                                        ( )
                                    [[repository]] => Users
                                )
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        ( [*] => 1 )
                    [[dirty]] => Array
                        ( )
                    [[original]] => Array
                        ( )
                    [[virtual]] => Array
                        ( )
                    [[errors]] => Array
                        ( )
                    [[invalid]] => Array
                        ( )
                    [[repository]] => Roles
                )
        )
)

```

This is the part of view where I iterate the array. Result is stored in $userList. There’s always more than one user in this array so I use a foreach loop.

```
<?php foreach ($userList as $user): ?>
<tr>
    <td><?= h($user->id) ?></td>
    <td><?= h($user->email) ?></td>
    <td><?= h($user->full_name) ?></td>
</tr>
<?php endforeach; ?>

```

In my view, the rows get generated but I don’t see the array values. If there are five users in the array, 5 rows are generated but values aren’t displayed. What’s wrong with the iteration I used? Can anyone point out my mistake? Thanks!

---

<div class="post-metadata">

### Author: ![joris](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/joris/32/338_2.png) [@joris](https://discourse.cakephp.org/u/joris)
#### Post date: [June 13, 2017, 8:30am UTC](https://discourse.cakephp.org/t/cakephp3-foreach-loop-iteration-failed/2705/2 "2017-06-13T08:30:18Z")

</div>

You’re iterating the array of Role Objects, not the User object.

```auto
<?php foreach ($userList as $user): ?>
<tr>
    <td><?= h($user->_matchingData['Users']->id) ?></td>
    <td><?= h($user->_matchingData['Users']->email) ?></td>
    <td><?= h($user->_matchingData['Users']->full_name) ?></td>
</tr>
<?php endforeach; ?>

```
