doucao1888 2019-05-09 19:42
浏览 46

在PHP中将MultiDimensional数组转换为父子表的有效方法

I am getting a result from a complex nested query that looks like this

   Heading 1      Heading 2            Heading 3          Heading 4
--------------------------------------------------------------------------
 - Total income  Total contributions    Total employers   Employer contribution 
 - Total income  Total contributions    Total participants   Other Contribution
 - Total income  Total contributions    
 - Total income  Total Earnings        Total dividends 
 - Total income  Total Earnings   
 - Total income 
 - Total Expenses Total benefit payments  Beniciearies payments 
 - Total Expenses Other Expense
 - Total Expenses

So I get this result set in a multidemensional array, here heading 1 is parent of heading 2 and heading 2 is parent of heading 3 and heading 3 is parent of heading 4. (similar to a nested menu-sub menu structure).

However the issue is that I want to convert it into a parent-child multidimensional array with parent-child ids which will look something like this

Array
(
    [0] = Array
        (
            [id] = 0
            [value] = Total Income
            [parent] = -1 /*Root Node should be set to -1 */
            [ttid] = 1
        )

    [1] = Array
        (
            [id] = 1
            [value] = Total Expenses
            [parent] = -1 /*Root Node */
            [ttid] = 2
        )

    [2] = Array
        (
            [id] = 2
            [value] = Total Contributions
            [parent] = 0
            [ttid] = 0
        )

    [3] = Array
        (
            [id] = 3
            [value] = Total Earning
            [parent] = 0
            [ttid] = 0
        )

    [4] = Array
        (
            [id] = 4
            [value] = Total Benifit Payments
            [parent] = 1
            [ttid] = 0
        )

    [5] = Array
        (
            [id] = 5
            [value] = Other Expense
            [parent] = 1
            [ttid] = 0
        )

    [6] = Array
        (
            [id] = 6
            [value] = Total Participants 
            [parent] = 2
            [ttid] = 0
        )

    [7] = Array
        (
            [id] = 7
            [value] = Total  Dividends
            [parent] = 3
            [ttid] = 0
        )

    [8] = Array
        (
            [id] = 8
            [value] = Benificiaries Payment
            [parent] = 4
            [ttid] = 0
        )

    [9] = Array
        (
            [id] = 9
            [value] = Directly to Participant
            [parent] = 4
            [ttid] = 0
        )

    [10] = Array
        (
            [id] = 10
            [value] = Other Contributions
            [parent] = 6
            [ttid] = 0
        )



)

To begin with, I started with a for loops on each of the headers to get all unqiue elements and then do iterations & string compare for every heading so as to do the parent-child match making.

I know this is an inefficient way to do it but this works for a small array of 5 elements. But it runs into 'Allocated Memory Exhausted' error with 100 records in multidmensional array.

How do I optimize this code or is there any better approach?





    $h1=array(); 
       $h2=array(); 
       $h3=array(); 
       $h4=array();

       for($i=0; $i < 5; ++$i)
       {
           $h1[$i]=$data[$i]['heading_1'];
           $h2[$i]=$data[$i]['heading_2'];
           $h3[$i]=$data[$i]['heading_3'];
           $h4[$i]=$data[$i]['heading_4'];
       }


           /*Get Unique Elements, Remove Duplicates, Renumber Index */

           $h1=array_values(array_unique($h1));
           $pc=array(); $k=1;

           /*Getting heading_1 as it is as they are root elements */
           for($i=0;$i<count($h1);++$i)
           {
             $pc[$i]['id']=$i;
             $pc[$i]['value'] = $h1[$i];
             $pc[$i]['parent'] =-1;
             $pc[$i]['ttid']=0;
           }

             /*Checking heading_1 of every row with h1 to determine parents of heading_2 */
          $th2=array();
             for($i=0;$i<count($pc);++$i)
             {

                 for($j=0;$j<5;++$j)
                 {
                    if (in_array($data[$j]['heading_2'],$th2))
                            continue; /* Skip redundant values */
                    else
                    {
                    if(strcmp($data[$j]['heading_1'],$pc[$i]['value'])==0)
                      {
                          $n=count($pc);
                          $pc[$n]['id'] = $n;
                          $pc[$n]['value'] = $data[$j]['heading_2'];
                          $pc[$n]['parent']= $i;
                          $pc[$n]['ttid']=0;
                          $th2[$i]=$data[$j]['heading_2'];

                      }
                    }

                 }
             }


             $th3=array();
             for($i=0;$i<count($pc);++$i)
             {

                 for($j=0;$j<5;++$j)
                 {
                    if (in_array($data[$j]['heading_3'],$th3))
                            continue; /* Skip redundant values */
                    else {        
                    if(strcmp($data[$j]['heading_2'],$pc[$i]['value'])==0)
                      {


                          $n=count($pc);
                          $pc[$n]['id'] = $n;
                          $pc[$n]['value'] = $data[$j]['heading_3'];
                          $pc[$n]['parent']= $i;
                          $pc[$n]['ttid']=0;
                          $th2[$i]=$data[$j]['heading_3'];

                      }
                    }

                 }
             }

              $th4=array();
             for($i=0;$i<count($pc);++$i)
             {
                 for($j=0;$j<5;++$j)
                 {
                    if (in_array($data[$j]['heading_4'],$th4))
                            continue;
                    else {        
                    if(strcmp($data[$j]['heading_3'],$pc[$i]['value'])==0)
                      {

                          $n=count($pc);
                          $pc[$n]['id'] = $n;
                          $pc[$n]['value'] = $data[$j]['heading_4'];
                          $pc[$n]['parent']= $i;
                          $pc[$n]['ttid']=0;
                          $th2[$i]=$data[$j]['heading_4'];

                      }
                    }

                 }
             } 




  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?