I'm working with an API and now i want to check if an array key exists then do something.
With an array i gather data like this:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
Then i use a get request like this:
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin');
And then i check if the array exists inside the results of the get request like this:
if(array_key_exists('first_name', $tet)) {
// do Nothing
}else{
// make API call
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
Im sure the get request is working, because i did a var_dump($tet); and i got this result:
array(3) { ["data"]=> array(6) { [0]=> array(18) { ["id"]=> string(39) "person:71cf33fb785433ab66550e5701120079
Well thats part of what i got, i cant post all of it. because it contains sensitive information.
For some reason even if the array_key exists this code still runs:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
--
array(3) { ["value"]=> bool(false) ["id"]=> string(41) "interest:7d3458131ea89afbe1bb3149bee8b668" ["name"]=> string(3) "Web" } } } } ["gender"]=> string(7) "Unknown" ["first_name"]=> string(5) "Kevin" ["family_name"]=> string(7) "A" ["full_name"]=> string(13) "Kevin A" ["email"]=> string(24) "ma@edd-marketingsuppxort.nl" ["phone"]=> string(8) "06269684" } [5]=> array(11) { ["id"]=> string(39) "person:51045f230a9cc39136aaf3a22a069eb0" ["interests"]=> array(1) { [0]=>
What are the first array keys of $tet?
We can see the first one is data, but what about the others one?
if(array_key_exists('first_name', $tet['data'])) {
//code...
}
edit: as @lma says array_key_exists can not dig into child, and even the edited structure you gave us look weird, some trailing } in the middle of the array.