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

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • jakub.skowron
    replied
    you can check if array is empty using empty() or is_null() php function

    Leave a comment:


  • mkas
    replied
    Thanks heaps all!
    I will give it ago!

    Leave a comment:


  • item
    replied
    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

    Leave a comment:


  • telecastg
    replied
    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.

    Leave a comment:


  • BattleMetalChris
    replied
    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'];
        }
    }

    Leave a comment:


  • mkas
    replied
    Can anybody help?

    Leave a comment:


  • 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.
Working...