php - 查找数组中是否存在键

标签 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));
    }
}

最佳答案

您从外部数组 ($tet['data']) 中选取数据元素并遍历其成员。在每个中,您使用 isset() 检查您要查找的 key 是否存在。或 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
    }
}

关于php - 查找数组中是否存在键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40883428/

相关文章:

php - 如何从发货ID或观察者处获取订单ID?

javascript - 如何过滤 native react 中的对象数组?

javascript - 数组 - 查找序列中缺失的数字

c++ - 是否可以使用枚举来访问数组?

php - 使用 regexr 工具识别 (weat)her

php - 当我在 PrestaShop 上创建产品时添加类别

php - Laravel 使用错误的所有者创建日志文件

python - 在 numpy 中 reshape ndarray 与常规数组?

java - 数组打印Java

php - Laravel 5.3 Eloquent where 从父模型连接表的子句