如何解决$ unset键上的键变量

时间:2016-02-01 23:45:35

标签: php wordpress undefined-index

我正在运行PHP脚本,并且不断收到错误:

  

注意:未定义的变量:键入D:\ 0-MYBLOG \ SERVER-MYBLOG \ InstantWP_4.3.1 \ iwpserver \ htdocs \ wordpress \ wp-content \ plugins \ sama-author-review \ admin \ metabox.php on line 310

310行看起来像这样:

unset( $new['items_review'][$key] );

这里是代码:

        if ( $new['items_review'] && is_array( $new['items_review'] )) {

            foreach ( $new['items_review'] as $review ) {

                if ( empty( $review['label'] ) ) {
                    unset( $new['items_review'][$key] );
                } else {
                    $review['value'] = absint( $review['value'] );
                    $review['label'] = esc_attr( $review['label'] );

                    if ( empty( $review['slug'] )) {
                        $review['slug']  = sanitize_title( $review['label'] );
                    } else {
                        $review['slug']  = sanitize_title( $review['slug'] );
                    }
                    $review['style'] = esc_attr( $review['style'] );
                    $items_review[$i] = $review;
                    $i++;
                }
            }   
        }

有解决这些错误的快速解决方法吗? 非常感谢任何帮助

谢谢

1 个答案:

答案 0 :(得分:3)

我认为我得到的问题是,您忘记将关键参数放在foreach声明中。

我承认它有点猜测,因为我不知道$new中的内容,但它确实存在逻辑可能性。

if ( $new['items_review'] && is_array( $new['items_review'] )) {

//    foreach ( $new['items_review'] as $review ) {
// replace foreach with this line

    foreach ( $new['items_review'] as $key => $review ) {

        if ( empty( $review['label'] ) ) {
            unset( $new['items_review'][$key] );
        } else {
            $review['value'] = absint( $review['value'] );
            $review['label'] = esc_attr( $review['label'] );
            if ( empty( $review['slug'] )) {
                $review['slug']  = sanitize_title( $review['label'] );
            } else {
                $review['slug']  = sanitize_title( $review['slug'] );
            }
            $review['style'] = esc_attr( $review['style'] );
            $items_review[$i] = $review;
            $i++;
        }
    }   
}