Php 用lambda函数递减函数码

Php 用lambda函数递减函数码,php,optimization,lambda,Php,Optimization,Lambda,我有一个函数,可以减少(优化)代码行的数量。此函数有一些重复的片段 是否可以使用lambda函数减少此代码 这是我的功能代码: static function get_all_category_with_widgets( $status = 'all' ) { $all_categories = SB_Central::get_categories(); $all_widgets = SB_Settings::get_sb_widgets()

我有一个函数,可以减少(优化)代码行的数量。此函数有一些重复的片段

是否可以使用lambda函数减少此代码

这是我的功能代码:

static function get_all_category_with_widgets( $status = 'all' ) {
            $all_categories = SB_Central::get_categories();
            $all_widgets    = SB_Settings::get_sb_widgets(); // Get all widgets from options variable.

            foreach ( $all_categories as $category_key => $category_value ) {
                foreach ( $all_widgets as $widget_value ) {
                    // Create one widget
                    $widget = array_merge($widget_value['widget'], array ( 'id' => $widget_value['id'], 'status' => $widget_value['status']));

                    // In this case save active and disable.
                    if ( $status == 'active_and_disable' && ( $widget_value['status'] == 'active' || $widget_value['status'] == 'disable' )) {
                        if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
                            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
                        }
                    } elseif ( $status == 'active' && $widget_value['status'] == 'active' ) {
                        if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
                            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
                        }
                    } elseif ( $status == 'disable' && $widget_value['status'] == 'disable' ) {
                        if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
                            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
                        }
                    } elseif ( $status == 'deleted' && $widget_value['status'] == 'deleted' ) {
                        if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
                            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
                        }
                    } elseif ( $status == 'all' ) {
                        if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
                            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
                        }
                    }
                }
            }

            return $all_categories;
        }
这是我一直重复的代码:

if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
                            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
                        } 

如果您知道减少此代码的更好方法,欢迎使用。

您可以替换If/elseif/elseif。。。。使用以下更简洁的表示法进行块

if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
    switch ($status){
        case "active_and_disable":
            // set if widget statis is active or disable
            if( $widget_value['status'] == 'active' || $widget_value['status'] == 'disable' ){
                $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
            }
            break;
        case "all":
            // always set
            $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
            break;
        default:
            // set when status == widget status
            if ($status == $widget_value['status']){
                $all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
            }
            break;
    }
}

所有的
IF
都可以组合成一个IF语句。请解释为什么你认为“更少的线条”总是与“优化”相关。@lightness Racess,它给我的印象是,我有一些重复的语句,从另一个角度看,我可以减少线条的数量。也许我解释得很好,但我想看到的是,如果可能的话,减少行数的可能性。谢谢你所做的一切。