2016-11-28 151 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

想通了,謝謝 –