2016-11-28 102 views
1

我有一个api,它从我的网站上的表单收集数据。 数据收集于一个这样的数组:检查名称是否存在php

$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 
); 

然后它就会像这样发:

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

我想,当阵列项目名称已经存在,使其呼应的东西。

所有新输入的数据被存储在一个JSON格式在这个网址:

/API/V2/CRM /组织

GET请求如下:

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

这里是我想要的伪代码的一个例子:

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

回答

1

使用array_key_exists()功能

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

另一个选项是isset()其也检查,如果变量不为空(设置)和empty()其检查变量是否存在不为空并且不为空。

+0

我想检查数组键名是否存在于mydomain.com/api/v2/crm/organization.json –

+0

所以json_decode()它到php数组并通过array_key_exists检查它 – Robert

+0

想通了,谢谢 –