drxm72811 2018-09-28 18:15
浏览 62
已采纳

从数组PHP中获取值 - 非法偏移量

This is the print_r from the $form_data variable / array in php log.

[28-Sep-2018 18:04:03 UTC] Array
(
    [cfdb7_status] => unread
    [meno] => data
    [email] => data
    [telefon] => phone
    [meno-ucastnika__1] => data
    [email-ucastnika__1] => data
    [meno-komory__1] => 
    [registracne-cislo__1] => 
    [_wpcf7_groups_count] => Array
        (
            [emails] => 1
        )

    [obchodne-meno] => obchod
    [obchodne-sidlo] => fs
    [ico] => 50426508
    [dic] => dic
    [icdph] => icdicko
)

How can I get the value of _wpcf7_groups_count key?

If I want email I simply wrote $form_data['email']. Everything goes like this except _wpcf7_groups_count.

$form_data['_wpcf7_groups_count']
$form_data['_wpcf7_groups_count'][0]['emails']
$form_data['_wpcf7_groups_count']['emails']

Anything from above doesn't work. The first is giving me an illegal offset.

  • 写回答

1条回答 默认 最新

  • douyunhuan9886 2018-09-28 18:19
    关注

    From the data you posted(*),

    $form_data['_wpcf7_groups_count']['emails']
    

    should work, and yield 1.

    Note that the parent key value is an array, so if the form data goes through some sort of templating engine, the problem might lie there.

    I find it strange that you get an error for the first method, and not for the others: in PHP, if you can't reference an array key, you cannot reference any of the descendants, and you still get the error from the parent key. This is what makes me suspect that something else is afoot.

    ==========

    (*) I assumed that you have deleted some information while keeping the formatting. Otherwise writing

    [meno-komory__1] => 
    [registracne-cislo__1] => 
    

    could possibly be interpreted as a nested key. I saw no 'Array', so I assumed there was just some data missing. But next time write it explicitly to avoid any ambiguity:

    [meno-komory__1] => "(redacted)",
    [registracne-cislo__1] => "(array, redacted)",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?