duanchun2349 2015-09-05 15:32
浏览 34
已采纳

函数只返回1个数组结果

I' am trying this for a long time now. The function below returns only 1 result where as it should return 3 results.

I have checked the database, the database returns results in a loop fine that means database part is okay, its somewhere in the function that am doing wrong.

//function that generates the URI from the database
function generate_uri( $menu_id = 0, $array = '' ){
    global $db;
    $array = array();
    if( !empty($menu_id) ){
        $db->where('menu_id', $menu_id);
        $menu = $db->ObjectBuilder()->getOne('menu');
        $menu_parent = $menu->menu_parent;
        $menu_slug = $menu->menu_slug;
        $array[] = $menu_slug;
        generate_uri($menu_parent, $array);
    }
    return $array;
}

//Calling the function with a parameter of 3
var_dump(generate_uri(3));

Output

array(1) { [0]=> string(15) "photo-gallery-1" }

Should Return

 array(1) { [0]=> string(15) "photo-gallery-1" [1]=> string(12) "photo-gallery" [2]=> string(9) "resources"}
  • 写回答

2条回答 默认 最新

  • douchang8219 2015-09-05 16:26
    关注

    Every time you call the function, you set $array empty: $array = array(); Another problem is that you are not getting the return of the function inside the if statement. Change this line:

    generate_uri($menu_parent, $array);
    

    To this:

    $array = generate_uri($menu_parent, $array);
    

    So, the function should be something like this:

    function generate_uri($menu_id = 0, $array = '') {
      global $db;
    
      if (!empty($menu_id)) {
        $db->where('menu_id', $menu_id);
        $menu = $db->ObjectBuilder()->getOne('menu');
        $menu_parent = $menu->menu_parent;
        $menu_slug = $menu->menu_slug;
        $array[] = $menu_slug;
        $array = generate_uri($menu_parent, $array);
      }
    
      return $array;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度