Answers.Body: <p>the way the api works now, the thing returned is an ArrayObject which you can call functions on, one of those is count()</p>
  
  <p>so just count the results; and then loop over them as you would if it was a normal array</p>
  
  <pre><code>  if (count($results) != 0) {
      foreach ($results as $result) {
        do things
      } 
  
    }                 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-28T18:58:59.433Z
Answers.LastActivityDate: 2019-06-28T18:58:59.433Z
Answers.UserDisplayName: ericG
Answers.Body: <p>See <a href="https://docs.civicrm.org/dev/en/latest/api/v4/usage/" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/api/v4/usage/</a> where it says:</p>
  <ul>
  <li>$result-&gt;count(): get the number of fetched (if LIMIT used) or
  matching results (if row_count included in select).</li>
  <li>From CiviCRM
  5.50+ $result-&gt;countFetched(): get the number of fetched results. If you used LIMIT this won't exceed that number (but could be less).</li>
  <li>From CiviCRM 5.50+ $result-&gt;countMatched(): get the total number of
  matching results. If you only fetched a subset (with LIMIT) you are
  required to add row_count to the select clause.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-20T13:48:59.913Z
Answers.LastActivityDate: 2023-03-20T13:48:59.913Z
Answers.UserDisplayName: Hershel
Body: <p>experimenting with refactoring some old code and figured I would experiment with apiv4. 
  the first hurdle was figuring out how to use the ArrayObject that is returned. </p>
  
  <p>in apiv3, for example I would look at the count value in the array. 
  in apiv4 there is no such value. </p>
  
  <p>my api v3 call looks like this</p>
  
  
  
  <pre><code>  $result = civicrm_api3('Contact', 'get', array(
    'return' =&gt; array("custom_90", "custom_91", "custom_85", "custom_88", "custom_143"),
      'id' =&gt; $alum_cid,
      'custom_143' =&gt; $sal['id'],
       ));   
    if ($result['count'] != 0) {
      do things
    }                 
  </code></pre>
  
  <p>my apiv4 call looks like this</p>
  
  
  
  <pre><code>$results = \Civi\Api4\Contact::get()
    -&gt;setSelect([
      'Employment_History.Salary_Yearly_', 
      'Employment_History.Currency', 
      'Employment_History.Employer', 
      'Employment_History.End_Date', 
      'Employment_History.relationshipID'
    ])
    -&gt;addWhere('id', '=', $alum_cid)
    -&gt;addWhere('Employment_History.relationshipID', '=', $sal['id'])
    -&gt;setLimit(25)
    -&gt;execute();
  </code></pre>
  
  <p>how do I get the count of the results?</p>
  
Tags: <api>
Title: how do I count results in API v4
ThreadText: # how do I count results in API v4
  
  (Asked by: username_0 on 2019-06-28)
  
  experimenting with refactoring some old code and figured I would experiment with apiv4. 
  the first hurdle was figuring out how to use the ArrayObject that is returned. 
  in apiv3, for example I would look at the count value in the array. 
  in apiv4 there is no such value. 
  my api v3 call looks like this
    $result = civicrm_api3('Contact', 'get', array(
    'return' => array("custom_90", "custom_91", "custom_85", "custom_88", "custom_143"),
      'id' => $alum_cid,
      'custom_143' => $sal['id'],
       ));   
    if ($result['count'] != 0) {
      do things
    }                 
  
  my apiv4 call looks like this
  $results = \Civi\Api4\Contact::get()
    ->setSelect([
      'Employment_History.Salary_Yearly_', 
      'Employment_History.Currency', 
      'Employment_History.Employer', 
      'Employment_History.End_Date', 
      'Employment_History.relationshipID'
    ])
    ->addWhere('id', '=', $alum_cid)
    ->addWhere('Employment_History.relationshipID', '=', $sal['id'])
    ->setLimit(25)
    ->execute();
  
  how do I get the count of the results?
  
  **Tags:** api
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2019-06-28. Score: 1 | Accepted answer
  
  the way the api works now, the thing returned is an ArrayObject which you can call functions on, one of those is count()
  so just count the results; and then loop over them as you would if it was a normal array
    if (count($results) != 0) {
      foreach ($results as $result) {
        do things
      } 
  
    }
  
  
  ---
  
  ### Answer by username_1 on 2023-03-20. Score: 2
  
  See <https://docs.civicrm.org/dev/en/latest/api/v4/usage/> where it says:
  
  * $result->count(): get the number of fetched (if LIMIT used) or
    matching results (if row\_count included in select).
  * From CiviCRM
    5.50+ $result->countFetched(): get the number of fetched results. If you used LIMIT this won't exceed that number (but could be less).
  * From CiviCRM 5.50+ $result->countMatched(): get the total number of
    matching results. If you only fetched a subset (with LIMIT) you are
    required to add row\_count to the select clause.
  
  
  ---
  
  