我从一个看起来像这样的复杂嵌套查询中得到结果
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
因此,我在多维数组中获得了该结果集,其中标题1是标题2的父级,标题2是标题3的父级,标题3是标题4的父级。(类似于嵌套菜单子菜单结构)
但是问题是我想将其转换为具有父子ID的父子多维数组,该子数组看起来像这样
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
)
)
首先,我从每个标头上的for循环开始,获取所有unueue元素,然后对每个标题进行迭代和字符串比较,以进行父子匹配。
我知道这是一种效率低下的方法,但这仅适用于5个元素的小型数组。但是它遇到多维数组中有100条记录的“分配的内存已耗尽”错误。
如何优化此代码,或者有什么更好的方法?
$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'];
}
}
}
}