Search code examples
phpforeachabstraction

Abstracting foreach function


I have created a function using foreach to store data in arrays (in this case, wordpress menu items):

public function beratungmenu()
{
    $array_menu = wp_get_nav_menu_items("205");
    $beratungmenu = array();
    foreach ($array_menu as $m) {
        if (empty($m->menu_item_parent)) {
            $beratungmenu[$m->ID] = array();
            $beratungmenu[$m->ID]['ID']          =   $m->ID;
            $beratungmenu[$m->ID]['title']       =   $m->title;
            $beratungmenu[$m->ID]['url']         =   $m->url;
            $beratungmenu[$m->ID]['postid']      =   get_post_meta( $m->ID, '_menu_item_object_id', true );
            $beratungmenu[$m->ID]['desc']        =   get_field('menu_item_desc', $m);
            $beratungmenu[$m->ID]['icon']        =   get_field('fa_icon_name', get_post_meta( $m->ID, '_menu_item_object_id', true ));
        }
    }
    $this_menu = wp_get_nav_menu_object("205");
    $beratungmenu['menu_title'] = $this_menu->name;      

    return $beratungmenu;
}

public function institutmenu()
{
    $array_menu = wp_get_nav_menu_items("206");
    $institutmenu = array();
    foreach ($array_menu as $m) {
        if (empty($m->menu_item_parent)) {
            $institutmenu[$m->ID] = array();
            $institutmenu[$m->ID]['ID']          =   $m->ID;
            $institutmenu[$m->ID]['title']       =   $m->title;
            $institutmenu[$m->ID]['url']         =   $m->url;
            $institutmenu[$m->ID]['postid']      =   get_post_meta( $m->ID, '_menu_item_object_id', true );
            $institutmenu[$m->ID]['icon']        =   get_field('fa_icon_name_institut', get_post_meta( $m->ID, '_menu_item_object_id', true ));
        }
    }
    $this_menu = wp_get_nav_menu_object("206");
    $institutmenu['menu_title'] = $this_menu->name;

    return $institutmenu;
}

The way I have to do it here is to create a seperate function for every menu, which is not very elegant as the functions do the same thing, just with a different array name.

For learning purposes, I am trying to find a way to abstract this idea one level further to be able to have only one function doing the same thing, passing the menu id ("205" and "206" in the example), retrieving the name of the menu, naming the array according to the menu name, and then doing what the functions above do.

How would this be possible?


Solution

  • You can fetch the menus like this:

    public function getMenu($menuId, $fieldName)
    {
        $array_menu = wp_get_nav_menu_items($menuId);
        $menu = array();
        foreach ($array_menu as $m) {
            if (empty($m->menu_item_parent)) {
                $menu[$m->ID] = array();
                $menu[$m->ID]['ID']          =   $m->ID;
                $menu[$m->ID]['title']       =   $m->title;
                $menu[$m->ID]['url']         =   $m->url;
                $menu[$m->ID]['postid']      =   get_post_meta( $m->ID, '_menu_item_object_id', true );
                $menu[$m->ID]['icon']        =   get_field($fieldName, get_post_meta( $m->ID, '_menu_item_object_id', true ));
            }
        }
        $this_menu = wp_get_nav_menu_object($menuId);
        $menu['menu_title'] = $this_menu->name;
    
        return $menu;
    }
    
    $beratungmenu = getMenu("205", "fa_icon_name");
    $institutmenu = getMenu("206", "fa_icon_name_institut");