Announcement

Collapse
No announcement yet.

API: How do I use IF statement (php) with 0 results in array?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API: How do I use IF statement (php) with 0 results in array?

    Hey everyone,

    Hoping this is easy and it is just me overlooking something simple.

    I have a GET api query which is running perfectly, however, I am trying to write a PHP IF statement for if the array is empty, like this:
    Code:
    Array ( [total] => 0[list] => Array ( ) )
    The concept, I am integrating my PBX system and have the values passing to my script. I want to search the Contacts phone numbers, and if the number is found, assign a value, if the number is not found I want it to run the same api get on the leads database.

    So in short.

    Phone number = 12345

    Get from contacts, ID where phone number = 12345 (if record found, set ContactID = ContactID)
    IF no contacts found get from Leads, ID where phone number = 12345 (if record found, set LeadID = LeadID)
    ELSE nothing.

    I can get it to work pefectly if the phone number is in the contact's by using the array $contactId = $response['list'][0]['id'], but don't know how to move it through the if statement when the 'total' in the array is 0.

    Any thoughts? Thanking you all muchly in advance.

  • #2
    Can anybody help?

    Comment


    • #3
      Code:
      $contactID = null;
      $leadID = null;
      
      $response = <stuff to get response from Contacts>
      if ($response && is_array($response['list']) && count($response['list'])>0) {
          $contactID = $response['list'][0]['id'];
      } else {
           $response = <stuff to get response from Leads>
          if ($response && is_array($response['list']) && count($response['list'])>0) { // assumes response from Leads is the same format as the response from Contacts
              $leadID = $response['list'][0]['id'];
          }
      }

      Comment


      • #4
        I think that you need to test for an empty array in your if else loop, something like this:

        Get from contacts, ID where phone number = 12345
        If response array is not empty and record is found {
        set ContactID = ContactID
        } else {
        get from leads, ID where phone number = 12345
        if response array is not empty and record is found {
        set LeadID = LeadID
        }
        }

        this link explains how testing for an empty array works https://www.geeksforgeeks.org/how-to...pty-using-php/
        Last edited by telecastg; 03-04-2021, 08:04 PM.

        Comment


        • #5
          Hello,

          i think in my memory :
          if ($result['total']) .. then. count($result['list'] ). give you the number of find.

          you can look

          foreach($result['list'] as $phoneNumber)

          Regards

          Comment


          • #6
            Thanks heaps all!
            I will give it ago!

            Comment


            • #7
              you can check if array is empty using empty() or is_null() php function

              Comment

              Working...
              X