I have searched for some time but I haven't found a good solution to this. There are similar posts for dealing with 1,2,3 level arrays but none that allows for an unknown level of arrays.
Aim: Cycle through x levels of multi dimensional arrays: if 1. value is array, continue cycle 2. is not an array close off cycle and output as a readable table.
I can do this manually by embedding foreach loops but whenever I try to build into a recursive function it melts.
Flat foreach example (which works for the array shown).
function doit($ARR){
if( is_array($ARR) )
{
foreach($ARR as $K => $ARR2)
{
if( is_array($ARR2) )
{
$t .= '<tr><td> > '.key($ARR).'</td>';
foreach($ARR2 as $K2 => $ARR3)
{
if( is_array($ARR3) )
{
$t .= '</tr><tr><td> >> '.key($ARR2).'</td>';
foreach($ARR3 as $K3 => $ARR4)
{
if( is_array($ARR4) )
{
$t .= '</tr><tr><td> >>> '.key($ARR3).'</td>';
foreach($ARR4 as $K4 => $ARR5)
{
if( is_array($ARR5) )
{
$t .= '</tr><tr><td> >>>> '.key($ARR4).'</td>';
foreach($ARR5 as $K5 => $ARR6)
{
if( is_array($ARR6) )
{
$t .= '</tr><tr><td> >>>>> '.key($ARR5).'</td>';
foreach($ARR6 as $K6 => $ARR7)
{
if( is_array($ARR7) )
{
$t .= '</tr><tr><td> >>>>>> '.key($ARR6).'</td>';
foreach($ARR7 as $K7 => $ARR8)
{
if( is_array($ARR8) )
{
$t .= '</tr><tr><td> >>>>>>> '.key($ARR7).'</td>';
}
else
{
$t .= '<td>'.$ARR8.'</td>';
}
}
}
else
{
$t .= '<td>'.$ARR7.'</td>';
}
}
}
else
{
$t .= '<td>'.$ARR6.'</td>';
}
}
}
else
{
$t .= '<td>'.$ARR5.'</td>';
}
}
}
else
{
$t .= '<td>'.$ARR4.'</td>';
}
}
}
else
{
$t .= '<td>'.$ARR3.'</td>';
}
}
}
else
{
$t .= '<td>'.$ARR2.'</td>';
}
}
}
else
{
}
$t .= '</tr>';
return '<table>'.$t.'</table>';
}
Example array to populate:
$IRS_EX_COVERAGE = array(
'Taxable returns:' => array(
'Individual income tax returns, total' => array(
'Returns with total positive income under $200,000 :' => array(
'Nonbusiness returns without Earned Income Tax Credit:' => array(
'Without Schedules C, E, F, or Form 2106' => array(79643929,262610,0.3,32922,229688),
'With Schedule E or Form 2106' => array(15997590,107300,0.7,49408,57892)
),
'Business returns without Earned Income Tax Credit:' => array(
'Nonfarm business returns by size of total gross receipts :' => array(
'Under $25,000' => array(10534942,94952,0.9,35192,59760),
'$25,000 under $100,000' => array(3124877,74825,2.4,24012,50813),
'$100,000 under $200,000' => array(877851,21724,2.5,13681,8043),
'$200,000 or more' => array(685163,13684,2.0,11549,2135)
),
'Farm returns' => array(1268251,4255,0.3,2475,1780)
),
'Business and nonbusiness returns with Earned Income' => array(
'Tax Credit by size of total gross receipts :' => array(
'Under $25,000' => array(6502703,459920,1.7,27009,432911),
'$25,000 or more' => array(1806228,18112,1.0,11161,6951)
)
)
),
'Returns with total positive income of at least $200,000 and under $1,000,000 :' => array(
'Nonbusiness returns' => array(4068298,71280,1.8,20795,50485),
'Business returns' => array(1734110,51151,2.9,17822,33329)
),
'Returns with total positive income of $1,000,000 or more' => array(416178,39753,9.6,13781,25972),
'International returns' => array(201097,8551,4.3,7269,1282),
)
)
);
$PRN_IRS = doit($IRS_EX_COVERAGE);
An example of the output table is shown here: http://taxformcalculator.com/IRS-audit-calculator.html