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'];
}
}
}
}