• This is function, that give multidimensional array (ierarhical) by menu ID. Need if you have custom html.

    function get_menu_array ($menu = array()) {
    $response = array();

    while ($menu) {
    // first level
    if (!$response) {
    foreach ($menu as $key => $item) {
    if ($item->menu_item_parent == 0) {
    $response[$item->db_id] = array(
    //’id’ => $item->db_id,
    ‘title’ => $item->title,
    ‘url’ => $item->url,
    ‘parent’ => $item->menu_item_parent,
    ‘target’ => $item->target,
    ‘attr_title’ => $item->attr_title,
    ‘description’ => $item->description,
    ‘classes’ => $item->classes,
    ‘xfn’ => $item->xfn,
    ‘children’ => array()
    );
    unset($menu[$key]);
    }
    }
    } else {
    foreach ($menu as $key => $item) {

    $temp_arr = array (
    ‘id’ => $item->db_id,
    ‘title’ => $item->title,
    ‘url’ => $item->url,
    ‘parent’ => $item->menu_item_parent,
    ‘target’ => $item->target,
    ‘attr_title’ => $item->attr_title,
    ‘description’ => $item->description,
    ‘classes’ => $item->classes,
    ‘xfn’ => $item->xfn,
    ‘children’ => array()
    );

    insert_children_in_array ($response,$temp_arr);

    unset($menu[$key]);
    }
    }
    }

    return $response;
    }

    function insert_children_in_array (&$arr, $temp_arr) {
    foreach ($arr as $key => $val ) {

    if ($key == $temp_arr[‘parent’]) {
    $arr[$key][‘children’][$temp_arr[‘id’]] = array(
    ‘title’ => $temp_arr[‘title’],
    ‘url’ => $temp_arr[‘url’],
    ‘parent’ => $temp_arr[‘parent’],
    ‘target’ => $temp_arr[‘target’],
    ‘attr_title’ => $temp_arr[‘attr_title’],
    ‘description’ => $temp_arr[‘description’],
    ‘classes’ => $temp_arr[‘classes’],
    ‘xfn’ => $temp_arr[‘xfn’],
    ‘children’ => array()
    );

    return $key;
    } else {

    if ($arr[$key][‘children’]) insert_children_in_array($arr[$key][‘children’], $temp_arr);
    }
    }
    }

    Example: get_menu_array(get_field(‘array’,’options’))

    • This topic was modified 6 years, 3 months ago by dimpa91.
  • The topic ‘Menu array’ is closed to new replies.