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条)

报告相同问题?

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决