Kevin.a Kevin.a - 1 year ago 102
reST (reStructuredText) Question

Check if name exists php

I have an api which posts gathered data from my form on my website.
The data is gathered with an array like this:

$org_payload = array(
'name' => $_POST['billing_company'],
'phone' => $_POST['billing_phone'],
'email' => $_POST['billing_email'],
'note' =>$_POST['order_comments'],
'relation_type' => array(
'id'=>'relationtype:c1ec3ae77036842d' //provide the relationtypeid, f.e. relationtype:796ce0d318a2f5db515efc18bba82b90
),
'visiting_address' => array(
'country_code' => 'NL',
'line_1' => $_POST['billing_address_1'],
'postal_code' => $_POST['billing_postcode'],
'locality' => $_POST['billing_city'],
'country' => $_POST['billing_country']

), // can be extented with other address data
'postal_address' => array(
'country_code' => 'NL'
) // can be extented with other address data
);


Then it gets sent like this:

$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));


I want that when the array item name already exists to make it echo something.

All the newly entered data gets stored in a json format in this url:


/api/v2/crm/organization


The get request looks like this:

$test = $SimplicateApi->makeApiCall('GET','/api/v2/crm/organization?q');


Here is an example of what i want in pseudo code:

if(name already exists){
echo 'this name already exists'
} else {
//Post it
$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));
}

Answer Source

use array_key_exists() function

if (array_key_exists('name', $org_payload)) {
   // do something
   echo 'this name already exists'
} else {
   // make API call
}

the other option are isset() which also check if variable is not null(is set) and empty() which checks whether variable exists is not null and is not empty.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download