<p>So, first you want to get the entry model using your entry id. In the EntriesService there is a method called <a href="http://buildwithcraft.com/docs/apis/entriesservice#getEntryById"><code>getEntryById</code></a> which you can use like so:</p>

<pre><code>$entry = craft()-&gt;entries-&gt;getEntryById(123);
</code></pre>

<p>This will return you an <a href="http://buildwithcraft.com/docs/templating/entrymodel">EntryModel</a> which has on it all your fields. So, if you matrix field handle is called <code>matrixFieldHandle</code> then you would access the field contents like so:</p>

<pre><code>$matrixBlocks = $entry-&gt;matrixFieldHandle;
</code></pre>

<p>A matrix field will return you an array of <a href="http://buildwithcraft.com/docs/templating/matrixblockmodel">MatrixBlockModels</a> from which you can get at everything else you need e.g.:</p>

<pre><code>foreach ($matrixBlocks as $block){
  echo $block-&gt;id;
  echo $block-&gt;fieldHandle;
}
</code></pre>

<p>Note that to play with block types you will need to look at the <a href="http://buildwithcraft.com/docs/apis/matrixservice">MatrixService</a> and <a href="http://buildwithcraft.com/docs/apis/matrixservice#getBlockTypeById"><code>getBlockTypeById()</code></a> to do things like filter out different blocks based on their block types.</p>

CC BY-SA 3.0
2014-08-07T16:16:02.690Z
It was that `$matrixBlocks = $entry->matrixFieldHandle;` I was missing. Thanks!!
Marion Newlevant
CC BY-SA 3.0
2014-08-07T16:29:19.957Z
No problem! Glad you got it sorted :)
Josh Angell
CC BY-SA 3.0
2014-08-07T11:34:17.767Z
2014-08-07T11:34:17.767Z
Josh Angell
<p>I'm writing a plugin where I have the id of an entry, that entry has a matrix field, and I want to iterate over the blocks of the matrix field. In php. How do I do that? Pointers to good documentation for how to do this would be appreciated too.</p>

<plugin-development><matrix>
How to iterate over matrix field from a plugin
# How to iterate over matrix field from a plugin

(Asked by: username_0 on 2014-08-07)

I'm writing a plugin where I have the id of an entry, that entry has a matrix field, and I want to iterate over the blocks of the matrix field. In php. How do I do that? Pointers to good documentation for how to do this would be appreciated too.

**Tags:** plugin-development, matrix


---

## 1 answers

### Answer by username_1 on 2014-08-07. Score: 7 | Accepted answer

So, first you want to get the entry model using your entry id. In the EntriesService there is a method called getEntryById which you can use like so:
$entry = craft()->entries->getEntryById(123);

This will return you an EntryModel which has on it all your fields. So, if you matrix field handle is called matrixFieldHandle then you would access the field contents like so:
$matrixBlocks = $entry->matrixFieldHandle;

A matrix field will return you an array of MatrixBlockModels from which you can get at everything else you need e.g.:
foreach ($matrixBlocks as $block){
  echo $block->id;
  echo $block->fieldHandle;
}

Note that to play with block types you will need to look at the MatrixService and getBlockTypeById() to do things like filter out different blocks based on their block types.

> **Comments:**
> **username_0**: It was that `$matrixBlocks = $entry->matrixFieldHandle;` I was missing. Thanks!!  
> **username_1**: No problem! Glad you got it sorted :)  

---

