2016-11-30 112 views
3

Im正在使用API​​。 隨着數組我收集數據,然後我把它發送到我正在使用的軟件。就像這樣:查找數組中是否存在密鑰

$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 

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

當我做一個GET調用,而不是POST這樣的:

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

然後var_dump($tet);

我相信它會返回一個所謂的多維數組:

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 
} 

我的問題是,我不想讓POST請求,如果數組鍵內部存在$tet

我嘗試first_name

$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

好吧,讓你挑'從外部陣列data'元素和遍歷其成員。在每一個你使用'isset()'或'array_key_exists()'來檢查你正在尋找的鍵是否存在。看起來很直截了當地對我... – arkascha

+0

我不是很高級的PHP有些事情不直接向我抱歉:L @arkascha –

+0

當然,我們都在學習。一切都好。你能遵循我的建議嗎? – arkascha

回答

3

您從外數組中挑選數據元素($tet['data'])並遍歷其成員。在每次檢查是否使用存在,你正在尋找的鑰匙要麼isset()array_key_exists()功能:

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 
    } 
} 

如果數組是非常大的,你想保留的內存消耗低,使得代碼秤也可以通過只遍歷數組鍵,而不是複數值防止數據的複製:基於在這裏對您的補充意見

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 
    } 
} 

它事實證明,你的實際問題是另一回事。這可能是足夠的,雖然添加一個簡單的區分大小寫字符串比較:

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

我知道如何解決它,但是在你的代碼中有一個錯誤解析錯誤:語法錯誤,意外''''\ n「' (T_CONSTANT_ENCAPSED_STRING),期待','或';' –

+0

@凱文。感謝您的提示,修正了它,這是我的愚蠢的鍵盤驅動程序堅持自動完成的結果;-) – arkascha

+0

仍然運行POST請求eventho我輸入的名稱存在$ tet內 –

1

您可以使用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

這會搜索_value_''first_name',OP指的是帶有該名稱的_key_ – arkascha

+1

當然,謝謝@arkascha的更正 – Muya

+0

更好,但'$ pers_payload'不是OP所指的數組。至少我是這樣理解這個問題的,只是讀了這個問題中的最後一句話...... – arkascha

相關問題