使用in_array返回不正确的索引


returning incorrect index using in_array

for循环正在遍历$data_statuses数组,我希望它输出epBwfEntry_update_submitted_epBwfNotify索引的值,因为它存在于$data中(复制了下面的数组结构)。我循环遍历该数组,并在使用$data_statuses数组找到匹配项后停止。

$data_statuses = array(
    'epBwfEntry_update_draft'                   =>  'draft',
    'epBwfEntry_create_draft'                   =>  'draft',
    'epBwfEntry_update_draft'                   =>  'draft',
    'epBwfEntry_update_submitted_epBwfNotify'   =>  'pending',
    'epBwfEntry_create_submitted_epBwfNotify'   =>  'pending',
    'epBwfDraft_create_submitted_epBwfNotify'   =>  'pending',
    'epBwfDraft_update_submitted_epBwfNotify'   =>  'pending',
);
foreach($data_statuses as $key => $value)
{
    if (in_array($key, $data))
    {
        print 'data: ' . $key . ': ' . $value . ' => ' . $data[$key]; 
        break;
    }
}
// index epBwfEntry_update_submitted_epBwfNotify exists in the $data array
// outputs: data: epBwfEntry_update_draft: draft =>
// expected output: epBwfEntry_update_submitted_epBwfNotify: pending => submitted
// $data array
Array
(
    [entry_id] => 3356
    [channel_id] => 1
    [autosave_entry_id] => 0
    [field_id_113] => YToxOntzOjE3OiJ0cmlnZ2VyX3JldmlzaW9ucyI7czoxOiIxIjt9
    [field_id_28] => 
    [field_id_103] => 
    [field_id_1] => 
    [field_id_79] => Yes
    [field_id_2] => 
    [field_id_3] => 
    [new_channel] => 1
    [epBwfEntry_update_submitted_epBwfNotify] => submitted
    [schedule_date_status] => no
    [draft_publish_date] => 01/19/2016
    [cp_call] => 1
    [revision_post] => Array
        (
            [entry_id] => 3356
            [channel_id] => 1
            [autosave_entry_id] => 0
            [filter] => 
            [structure__parent_id] => 2927
            [structure__template_id] => 146
            [structure__hidden] => n
            [title] => testtttt
            [structure__uri] => testtttt
            [url_title] => testtttt
            [expiration_date] => 
            [structure__listing_channel] => 0
            [field_id_113] => Array
                (
                    [trigger_revisions] => 1
                )
            [field_id_28] => Array
                (
                    [0] => 
                )
            [field_id_103] => 
            [field_id_1] => 
            [field_id_79] => Yes
            [field_id_2] => 
            [field_id_3] => 
            [entry_date] => 1452194340
            [new_channel] => 1
            [author] => 112
            [versioning_enabled] => y
            [epBwfEntry_update_submitted_epBwfNotify] => submitted
            [schedule_date_status] => no
            [draft_publish_date] => 01/19/2016
        )
    [options] => 
    [author] => 
)

in_array检查要搜索的数组中是否存在。您不是在搜索值,而是在搜索密钥,因此您需要(例如…):

if (array_key_exists($key, $data)) {
    ...

注意,这不会在数组中的值中搜索关键字,这些值本身就是数组,但这同样适用于in_array;两者都只搜索数组的第一级。