2016-11-30 3 views
3

Ich arbeite mit einer API. Mit einem Array sammle ich Daten und dann sende ich es an die Software, mit der ich arbeite. Wie folgt aus:Suchen, ob Schlüssel im Array existiert

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

Dann schicke ich es wie dieses

// add the person to CRM 

    $person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload)); 

Wenn ich einen GET-Aufruf tun statt POST wie folgt aus:

$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1'); 

und dann var_dump($tet);

I glaube, es gibt ein sogenanntes multidimensionales Array zurück:

array(3) { 
    ["data"] => array(2) { 
     [0] => array(11) { 
      ["id"] => string(39) 
      "person:067af3bd2045824e62ac579e634623b8" ["interests"] => array(1) { 
       [0] => array(3) { 
        ["value"] => bool(false)["id"] => string(25) 
        "interest:f278f47e6e9d48b8" ["name"] => string(19) 
        "Actief in Duitsland" 
       } 
      }["simplicate_url"] => string(51) 
      "https://emark.simplicate.nl/crm/person/view?id=3552" ["avatar"] => array(2) { 
       ["initials"] => string(2) 
       "Kt" ["color"] => string(7) 
       "#03e084" 
      }["linked_as_contact_to_organization"] => array(1) { 
       [0] => array(7) { 
        ["id"] => string(46) 
        "contactperson:0f16f418f1845749c79bebf9e1e753e5" ["organization_id"] => string(45) 
        "organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9) 
        "testing12" ["work_email"] => string(24) 
        "[email protected]" ["work_phone"] => string(8) 
        "06269684" ["work_mobile"] => string(8) 
        "06269684" ["interests"] => array(16) { 
         [0] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:456e8b19c0079647" ["name"] => string(11) 
          "Twinkle 100" 
         }[1] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:a70e69b83382e85a" ["name"] => string(17) 
          "Bekend merk in NL" 
         }[2] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:ce50f1b5593ac180" ["name"] => string(15) 
          "Cross Border 30" 
         }[3] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:f40eca1b281969d6" ["name"] => string(20) 
          "Meerdere vestigingen" 
         }[4] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:7435d7409a07cefb" ["name"] => string(26) 
          "Meer dan 100k in Duitsland" 
         }[5] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:cc072cea856ea23a" ["name"] => string(17) 
          "B2B leadgeneratie" 
         }[6] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:f278f47e6e9d48b8" ["name"] => string(19) 
          "Actief in Duitsland" 
         }[7] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:9bbeb23d17283595" ["name"] => string(10) 
          "Exporteert" 
         }[8] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:97ed988af66b1abc" ["name"] => string(8) 
          "Debiteur" 
         }[9] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9) 
          "Marketing" 
         }[10] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6) 
          "Amazon" 
         }[11] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3) 
          "Jur" 
         }[12] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11) 
          "Vertalingen" 
         }[13] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5) 
          "Adres" 
         }[14] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4) 
          "GmbH" 
         }[15] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3) 
          "Web" 
         } 
        } 
       } 
      }["gender"] => string(7) 
      "Unknown" ["first_name"] => string(6) 
      "Kevin1" ["family_name"] => string(7) 
      "testing" ["full_name"] => string(14) 
      "Kevin1 testing" ["email"] => string(24) 
      "[email protected]" ["phone"] => string(8) 
      "06269684" 
     }[1] => array(11) { 
      ["id"] => string(39) 
      "person:067af3bd2045824ea8c16e7ea0baf9d6" ["interests"] => array(1) { 
       [0] => array(3) { 
        ["value"] => bool(false)["id"] => string(25) 
        "interest:f278f47e6e9d48b8" ["name"] => string(19) 
        "Actief in Duitsland" 
       } 
      }["simplicate_url"] => string(51) 
      "https://emark.simplicate.nl/crm/person/view?id=3553" ["avatar"] => array(2) { 
       ["initials"] => string(2) 
       "Kt" ["color"] => string(7) 
       "#dce1f3" 
      }["linked_as_contact_to_organization"] => array(1) { 
       [0] => array(7) { 
        ["id"] => string(46) 
        "contactperson:f48fdcaaff0211e728a2e4ccf197900b" ["organization_id"] => string(45) 
        "organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9) 
        "testing12" ["work_email"] => string(24) 
        "[email protected]" ["work_phone"] => string(8) 
        "06269684" ["work_mobile"] => string(8) 
        "06269684" ["interests"] => array(16) { 
         [0] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:456e8b19c0079647" ["name"] => string(11) 
          "Twinkle 100" 
         }[1] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:a70e69b83382e85a" ["name"] => string(17) 
          "Bekend merk in NL" 
         }[2] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:ce50f1b5593ac180" ["name"] => string(15) 
          "Cross Border 30" 
         }[3] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:f40eca1b281969d6" ["name"] => string(20) 
          "Meerdere vestigingen" 
         }[4] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:7435d7409a07cefb" ["name"] => string(26) 
          "Meer dan 100k in Duitsland" 
         }[5] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:cc072cea856ea23a" ["name"] => string(17) 
          "B2B leadgeneratie" 
         }[6] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:f278f47e6e9d48b8" ["name"] => string(19) 
          "Actief in Duitsland" 
         }[7] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:9bbeb23d17283595" ["name"] => string(10) 
          "Exporteert" 
         }[8] => array(3) { 
          ["value"] => bool(false)["id"] => string(25) 
          "interest:97ed988af66b1abc" ["name"] => string(8) 
          "Debiteur" 
         }[9] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9) 
          "Marketing" 
         }[10] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6) 
          "Amazon" 
         }[11] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3) 
          "Jur" 
         }[12] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11) 
          "Vertalingen" 
         }[13] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5) 
          "Adres" 
         }[14] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4) 
          "GmbH" 
         }[15] => array(3) { 
          ["value"] => bool(false)["id"] => string(41) 
          "interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3) 
          "Web" 
         } 
        } 
       } 
      }["gender"] => string(7) 
      "Unknown" ["first_name"] => string(6) 
      "Kevin1" ["family_name"] => string(7) 
      "testing" ["full_name"] => string(14) 
      "Kevin1 testing" ["email"] => string(24) 
      "[email protected]" ["phone"] => string(8) 
      "06269684" 
     } 
    }["errors"] => NULL["debug"] => NULL 
} 

Mein Problem ist, ich möchte nicht die POST-Anforderung machen, wenn die Array-Schlüssel first_name innen existiert $tet

Meine Versuche:

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

// add the person to CRM 
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1'); 

foreach ($tet['data'] as $data) { 
    if (array_key_exists('first_name', $data)) { 
     echo "found key 'first_name' with value '" . $data['first_name'] . "'\n"; 
    } else { 
     // perform your post request 
     $person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload)); 
    } 
} 
+0

Nun, so wählen Sie das 'data' Element aus dem äußeren Array und iterieren über seine Mitglieder. In jedem Fall prüfen Sie, ob der Schlüssel, nach dem Sie suchen, mit 'isset()' oder 'array_key_exists()' existiert. Sieht für mich ziemlich direkt aus ... – arkascha

+0

Ich bin nicht wirklich fortgeschritten in php einige Dinge schauen nicht direkt nach vorne zu mir Entschuldigung: L @arkascha –

+0

Sicher, wir alle lernen die ganze Zeit. Alles gut. Kannst du dem folgen, was ich vorgeschlagen habe? – arkascha

Antwort

3

Sie wählen das Datenelement aus dem äußeren Array ($tet['data']) und iterieren über seine Mitglieder. In jeder überprüfen Sie, ob der Schlüssel die Sie suchen, existiert entweder mit dem isset() oder die array_key_exists() Funktion:

foreach ($tet['data'] as $data) { 
    if (array_key_exists('first_name', $data)) { 
     echo "found key 'first_name' with value '" . $data['first_name'] . "'\n"; 
    } else { 
     // perform your post request 
    } 
} 

Wenn das Array ist wirklich groß, und Sie wollen den Speicherverbrauch niedrig zu halten, so dass die Code Skalen gut können Sie das Kopieren der Daten verhindern, indem sie nur über die Array-Schlüssel iterieren, nicht die komplexen Werte:

foreach (array_keys($tet['data']) as $key) { 
    if (array_key_exists('first_name', $tet['data'][$key])) { 
     echo "found key 'first_name' with value '" . $tet['data'][$key]['first_name'] . "'\n"; 
    } else { 
     // perform your post request 
    } 
} 

Basierend auf weitere Kommentare hier sind es Es stellte sich heraus, dass Ihre eigentliche Frage eine andere ist. Es ist wahrscheinlich genug, um einen einfachen Vergleich ohne Berücksichtigung der Groß-/Kleinschreibung hinzuzufügen:

foreach (array_keys($tet['data']) as $key) { 
    if ( array_key_exists('first_name', $tet['data'][$key]) 
     && (strcasecmp($tet['data'][$key]['first_name'], 'Kevin1') == 0)) { 
     echo "found key 'first_name' with value '" . $tet['data'][$key]['first_name'] . "'\n"; 
    } else { 
     // perform your post request 
    } 
} 
+0

ich weiß, wie man es löst, aber es gibt einen Fehler in Ihrem Code Parse error: Syntaxfehler, unerwartet '"' \ n" ' (T_CONSTANT_ENCAPSED_STRING), erwartet ',' oder ';' –

+0

@ Kevin.a Danke für den Hinweis, behoben, das war ein Ergebnis meines dummen Tastaturtreibers, der auf automatischer Vervollständigung besteht ;-) – arkascha

+0

läuft immer noch das POST-Anfrage-Ereignis Der Name ich gebe existiert innerhalb $ tet –

1

Sie können array_key_existsfunction

if (array_key_exists('first_name', $pers_payload)) { 
 
    echo 'Can not perform this task'; 
 
}else{ 
 
\t $person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload)); 
 
}

+0

Dies würde nach einem _value_ '' first_name 'suchen, das OP verweist auf einen _key_ mit diesem Namen – arkascha

+1

Sicher, danke @arkascha für die Korrektur – Muya

+0

Besser, aber '$ pers_payload' ist nicht das Array, auf das sich das OP bezieht. Zumindest habe ich die Frage verstanden. Lesen Sie einfach den letzten Satz in der Frage ... – arkascha

Verwandte Themen