PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-查找数组中是否存在键大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用API​​.
通过阵列,我可以收集数据,然后将其发送到正在使用的软件中.像这样:

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

然后我像这样发送

@H_696_5@// add the person to CRM $person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));

当我像这样进行GET呼叫而不是POST时:

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

然后是var_dump($tet);

我相信它会返回所谓的多维数组:

@H_696_5@ 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) "ma@e-markeTingsupport.nl" ["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) "ma@e-markeTingsupport.nl" ["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) "ma@e-markeTingsupport.nl" ["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) "ma@e-markeTingsupport.nl" ["phone"] => String(8) "06269684" } }["errors"] => NULL["debug"] => NULL }

我的问题是,如果数组键first_name存在于$tet中,我不想发出POST请求.

我的尝试:

@H_696_5@$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)); } }

解决方法:

您从外部数组($tet [‘data’])中选择数据元素并对其成员进行迭代.在每一个中,您都使用isset()array_key_exists()函数检查要查找的键是否存在:

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

如果数组确实很大,并且您希望保持较低的内存消耗,以便代码可以很好地扩展,则可以仅通过遍历数组键而不是复杂值来防止数据复制:

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

根据您在此处的其他评论,事实证明您的实际问题是另外一个问题.不过,添加一个不区分大小写的简单字符串比较可能就足够了:

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

大佬总结

以上是大佬教程为你收集整理的php-查找数组中是否存在键全部内容,希望文章能够帮你解决php-查找数组中是否存在键所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: