I am so stumped.
Trying to establish a Chart of Accounts array (bookkeeping) "COA" (with opening account balances) via post vars from a COA Interview-GUI (form).
Beginning with a base COA array -- including primary account-names (as keys) and single-item sub-arrays containing only account-base-numbers as the singlular sub-array values for each -- I'm having difficulty parsing the post vars (sub-account values) into the desired sub-account arrays in my COA array.
I'm close, but not quite there. Reduced to chasing my tail.
Ok... Clearly I wish that made better sense, so let me try it another way before presenting my code below.
First a sample $_POST containing data for two primary accounts (cash_bank, and fixed_assets), five first tier sub accounts(checking, paypal, petty_cash, equipment, and furniture_and_fixtures), along with data for four second tier sub-accounts (Office_Computer, Office_Printer, Desk, and Desk_Chair)
$_POST = array(
"cash_bank~checking" => "875.23",
"cash_bank~paypal" => "225.25",
"cash_bank~petty_cash" => "12.00",
"fixed_assets~equipment~Office_Computer" => "300.00",
"fixed_assets~equipment~Office_Printer" => "150.00",
"fixed_assets~furniture_and_fixtures~Desk" => "300.00",
"fixed_assets~furniture_and_fixtures~Desk_Chair" => "180.00",
);
Secondly, an abbreviated beggining (base) COA array
$coa = array(
'cash_bank'=>array(100),
'fixed_assets'=>array(150),
);
Third Sample (almost there) Output from half-baked script below...
Array
(
[cash_bank] => Array
(
[0] => 100
[1] => checking
[2] => paypal
[3] => petty_cash
)
[fixed_assets] => Array
(
[0] => 150
[1] => Array
(
[equipment] => Array
(
[0] => Office_Computer
[1] => Office_Printer
)
[furniture_and_fixtures] => Array
(
[0] => Desk
[1] => Desk_Chair
)
)
[2] => equipment
[3] => furniture_and_fixtures
)
)
The idea here, is to codify account numbers for each sub-account whereby the account number for the cash_bank sub accounts "paypal and petty_cash" would be 100-2 and 100-3 respectively. As you can see this works as desired for these first tiered sub-accounts, however I haven't been able to sort it out for second tier sub-accounts such as those under the fixed_assets account where the Desk_Chair account-number should resolve to 150-2-1, but as you can see... it does not.
if you've made it this far... WOW Thanks! Hopefully that's a little clearer?
If you're still with me, Thanks again, and here's my sample script in total.
<?php
$_POST = array(
"action" => "init_coa",
"cash_bank~checking" => "875.23",
"cash_bank~paypal" => "225.25",
"cash_bank~petty_cash" => "12.00",
"fixed_assets~equipment~Office_Computer" => "300.00",
"fixed_assets~equipment~Office_Printer" => "150.00",
"fixed_assets~furniture_and_fixtures~Desk" => "300.00",
"fixed_assets~furniture_and_fixtures~Desk_Chair" => "180.00",
);
$coa = array(
'cash_bank'=>array(100),
'fixed_assets'=>array(150),
);
foreach($_POST as $key => $value)
{ $sub_1 = $sub_2 = '';
$subs = explode('~',$key);
if(count($subs)==1) $account = $key;
else
{ $account = $subs[0];
$sub_1 = $subs[1];
if(count($subs)>2) $sub_2 = $subs[2];
}
$acctbase_no = $coa[$account][0];
if($sub_1 && !in_array($sub_1,$coa[$account])){ $coa[$account][] = $sub_1;}
if($sub_2)
{ if(!@$sbky) $sbky = array_pop(array_keys($coa[$account]));
if(!is_array($coa[$account][$sbky]))
$coa[$account][$sbky] = array($coa[$account][$sbky]=>array($sub_2));
else $coa[$account][$sbky][$sub_1][] = $sub_2;
}
}
echo "<pre>"; print_r($coa); echo "</pre>";
?>