the find method return value

I have a problem when I using the method find in models.

if I have a table called user like this :

id name

1 linbob

2 phalcon

3 framework

My code:

$userData = User::find();

foreach($userData as $key => $data) {

 echo 'key is '.$key.' and user is '.$data->name.'<br />';


the output is :




but if I have the code :

$userData = User::find();

echo $userData[-1]->name;

echo $userData[-100]->name;

why the output is the first value and why this key has value?

In your example $userData isn't actually an array, it's an object that is probably extending the ArrayIterator class

My guess is that using negative array indices is either undefined, or is defined as just returning the first item.