API: How do I use IF statement (php) with 0 results in array?
Collapse
X
-
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)
RegardsLeave a comment:
-
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:
-
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:
-
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 ( ) )
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.Tags: None
Leave a comment: