简体   繁体   English

PHP:具有空值的数组在foreach中给出错误

[英]PHP: array with empty value gives error in foreach

For this example, when I call the variable $my_settings , the output would look like this: 对于此示例,当我调用变量$my_settings ,输出将如下所示:

Array (
    [personal_options] => Array (
            [rich_editing] => rich_editing
            [admin_color] => admin_color
            [comment_shortcuts] => comment_shortcuts
            [admin_bar_front] => admin_bar_front
        )
    [name] => Array (
            [nickname] => nickname
        )
    [contact_info] => Array (
            [url] => url
        )
    [about_yourself] => Array (
            [description] => description
        )
    [yoast_seo] => 
)

When I run a foreach loop, get everyone's favorite " Invalid argument supplied for foreach() " error because this array has [yoast_seo] => , which is empty and throwing it off. 当我运行一个foreach循环时,请获取每个人最喜欢的“ 为foreach()提供了无效参数 ”错误,因为此数组具有[yoast_seo] => ,该值为空并将其丢弃。

Currently my foreach is set up as the following: 目前,我的foreach设置如下:

$my_settings = get_option( 'dsbl_profile_settings' );

if ( is_array( $my_settings ) || is_object( $my_settings ) ) {
    foreach ( $my_settings as $group => $item ) {
        foreach ( $item as $value ) {
            echo '<pre>'; print_r( $value ); echo '</pre>';
        }
    }
}

As you can see, I already use the is_array() and is_object() check in my loop. 如您所见,我已经在循环中使用了is_array()is_object()检查。 My guess is that I need to also perform a check to see if it's empty as well before it runs [yoast_seo] => ? 我的猜测是,在运行[yoast_seo] =>之前,我还需要执行一次检查以查看其是否为空? I'm at a loss on the best way to implement that since I've tried the following in my if statement: 由于在if语句中尝试了以下操作,因此我对实现该方法的最佳方法感到茫然:

if ( is_array( $profile_fields ) || is_object( $profile_fields ) || isset( $profile_fields ) ) { // Attempt #1

if ( ( is_array( $profile_fields ) || is_object( $profile_fields ) ) && isset( $profile_fields ) ) { // Attempt #2

It's because you have nested foreach and you are providing an empty variable, you should check if the variable is array before passing. 这是因为您嵌套了foreach,并且要提供一个空变量,所以在传递之前,应检查该变量是否为数组。

if ( is_array( $my_settings ) || is_object( $my_settings ) ) {
    foreach ( $my_settings as $group => $item ) {
        if(is_array($item)) {
            foreach ( $item as $value ) {
                echo '<pre>'; print_r( $value ); echo '</pre>';
            }
        }
    }
}

You have checked is_array( $my_settings ) for $my_settings , which is correct. 您已检查is_array( $my_settings )是否有$my_settings ,这是正确的。 But what about foreach ( $item as $value ) ? 但是foreach ( $item as $value )呢?

Your error is there for the group level loop. 组级循环存在您的错误。 Not for $my_settings . 不适用于$my_settings

So if you do 所以如果你这样做

if ( is_array( $my_settings ) || is_object( $my_settings ) ) {
    foreach ( $my_settings as $group => $item ) {
        if ( !empty($item) && (is_array( $item ) || is_object( $item )) ) {
            foreach ( $item as $value ) {
                echo '<pre>'; print_r( $value ); echo '</pre>';
            }
        }
    }
}

it should work. 它应该工作。 Basically same condition that you are checking for $my_settings . 与检查$my_settings基本上相同的条件。

Hope this helps you! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM