2016-11-28 2 views
1

Ich habe eine API, die gesammelten Daten aus meinem Formular auf meiner Website veröffentlicht. Die Daten werden mit einem Array wie folgt gesammelt:Überprüfen, ob der Name existiert php

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

Ich möchte, dass, wenn der Array-Element Name schon machen existiert es etwas echo:

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

Dann es so geschickt wird.

die neu Alle eingegebenen Daten wird in einem JSON-Format in dieser URL gespeichert:

/api/v2/crm/Organisation

Die Anfrage get wie folgt aussieht:

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

Hier ist ein Beispiel, was ich in Pseudocode wollen:

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

Antwort

1

Verwendung array_key_exists() Funktion

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

die andere Option sind isset() die auch überprüfen, ob die Variable nicht null ist (gesetzt) ​​und empty() die, ob die Variable überprüft existiert nicht null und ist nicht leer.

+0

Ich wollte überprüfen, ob der Array-Schlüssel-Namen innerhalb mydomain.com/api/v2/crm/organization.json –

+0

so json_decode() es zu PHP Array und überprüfen Sie es über Array_key_exists – Robert

+0

fand es heraus, danke –

Verwandte Themen