Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get specific result from object by index


I get all rows from the database with $data = Data::find(); and I bind them to a var for the views ($this->view->setVar("data", $data);).

Now in the views I want the data from row 6. How can I get the data by index? As it is an object and not an array..

I don't want to run a for each it.


Just access the sixth position:

echo $data[6]->id;


Or Data::find(6) if you only want one row, isn't it?


I think I made the question too general.

In reality I have a few where and a order within it. So Data::find(6) wouldn't work.

@Phalcon what would be the volt syntax for echo $data[6]->id; ?


It would be:

{{ data[6].id }}


@Phalcon I tried that before and it gives me: Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in...


Are you using PHP 5.3?


Could you please open the generated file and post the generated code?


@Phalcon this is the generated output: <h3><?php echo ($data[5])->id; ?></h3>

edited Aug '14

I think PHP doesn't like that syntax: you can use {% set value = data[6] %} {{ }}

edited Aug '14

mhh, ok thanks:

What I did to fix it temp was to convert the object with a foreach loop to an array in the controller and than I can access everything fine with the volt syntax.

I think I will use the <?php ?> way than. Seems to be the cleanest one for me.