查找数组中是否存在键

时间:2016-11-30 08:22:16

标签: php arrays

我正在使用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)
                    "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请求

我的尝试:

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

2 个答案:

答案 0 :(得分:3)

从外部数组($tet['data'])中选择数据元素并迭代其成员。在每个中,使用documentationPRAGMA writable_schema函数检查您要查找的密钥是否存在:

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

答案 1 :(得分:1)

您可以使用array_key_exists function

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