Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T16:16:02.690Z
Answers.Text: It was that `$matrixBlocks = $entry->matrixFieldHandle;` I was missing. Thanks!!
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T16:29:19.957Z
Answers.Text: No problem! Glad you got it sorted :)
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T11:34:17.767Z
Answers.LastActivityDate: 2014-08-07T11:34:17.767Z
Answers.UserDisplayName: Josh Angell
Body: <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>
  
Tags: <plugin-development><matrix>
Title: How to iterate over matrix field from a plugin
ThreadText: # 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`](http://buildwithcraft.com/docs/apis/entriesservice#getEntryById) which you can use like so:
  
  ```
  $entry = craft()->entries->getEntryById(123);
  
  ```
  
  This will return you an [EntryModel](http://buildwithcraft.com/docs/templating/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](http://buildwithcraft.com/docs/templating/matrixblockmodel) 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](http://buildwithcraft.com/docs/apis/matrixservice) and [`getBlockTypeById()`](http://buildwithcraft.com/docs/apis/matrixservice#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 :)  
  
  ---
  
  