donglu7998 2016-01-17 15:06
浏览 39
已采纳

在php中递归创建关联数组 - 使用echo“工作”

I have a database with modules and submodules.

The function getChildren() is supposed to return the association between them to create a navigation element:

public function getChildren($id=0) {
        $modulesResult = $this->find('all', array(
            'conditions' => array('parent' => $id)
        ));
        echo "<ul>";
        foreach ($modulesResult as $key => $value) {
            echo "<li>";
            echo $value['module']['id'].". ".$value['module']['modulename'];
            $this->getChildren($value['module']['id']);
            echo "</li>";
        }
        echo "</ul>";
    }

Which produces the following output:

1. Calendar
2. Mailbox
3. Statistics
     15. Local Statistics
     16. Regiona Sstatistics
     17. Global Statistics
     18. Custom Statistical Reports
4. Production
     19. Current Production
     22. Sort By Product
     23. Create Product
     24. List Products
     20. Production History
     25. From-To
     26. Yearly
     27. Monthly
     21. Projections
     28. Analysis
5. Trade
     29. E-store
     30. Products
     32. List Products
     33. Create Product
     34. Sort By Product
     31. Dashboard

I want to do the same thing, but instead of echo, I want to put everything in an associative array.

I tried this:

public function getChildrenTest($id=0, $array=array()) {
        pr($array);
        $modulesResult = $this->find('all', array(
            'conditions' => array('parent' => $id)
        ));
        foreach ($modulesResult as $key => $value) {
            #echo $value['module']['id'].$value['module']['modulename']."<br>";
            $array[$value['module']['parent']][$value['module']['id']] = $value['module']['modulename'];
            $this->getChildrenTest($value['module']['id'], $array);
        }
        return $array;
    }

But it outputs only the first level, like this:

Array
(
    [0] => Array
        (
            [1] => Calendar
            [2] => Mailbox
            [3] => Statistics
            [4] => Production
            [5] => Trade
        )
)

I tried several variations of the code above but nothing works! Seems am stuck!

Am I losing some information on the recursion?

  • 写回答

1条回答 默认 最新

  • dougezhua0017 2016-01-17 15:42
    关注

    Try the following:

    public function getChildren($id=0) {
        $modulesResult = $this->find('all', array(
            'conditions' => array('parent' => $id)
        ));
    
        $array=[];
        foreach ($modulesResult as $key => $value) {
    
            $array[$value['module']['id']]=array(
                $value['module']['modulename']=>$this->getChildren($value['module']['id'])
            );
        }
    
        return $array;
    }
    

    Or you may as well use find('threaded'), which is the finder CakePHP provides to work with nested results:

    $modulesResult = $this->find('threaded',['parent'=>'parent']);
    

    Which equals to:

    $modulesResult = $this->find('all');
    $modulesResult = Hash::nest($modulesResult,['parentPath'=>'{n}.module.parent']);
    

    See:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式