0

I'm building tax calculator, and I have been able to build a JSON object for one key. I need to extend it to have 3 more objects, but am have having trouble.

Currently, I can do net income doing this:

$a       = array();
$a_json      = array();
$a_json_row  = array();
$a_json_row2  = array();
$a_json_final      = array();


if ($queryIndexData) {
    while($row = mysql_fetch_array($queryIndexData)) {
        $person      = $row['person'];
        $netincome = $row['NetIncome'];
        $statetax  = $row['StateTax'];
        $fedtax    = $row['FedTax'];
        $totaltax  = $row['TotalTax'];

        $a = array(
            'label' => $person,
            'value' => $netincome * 1,
        );
        array_push($a_json, $a);


     }
    $a_json_row2[key] = 'Net Income';
    $a_json_row2[values] = $a_json;
    array_push($a_json_final, $a_json_row2);

// jQuery wants JSON data
echo json_encode($a_json_final);

It's not very clean and I hard coded the key. How can I dynamically generate the key from my $netincome, $statetax, $fedtax, and $totaltax variable?

so my JSON looks like:

var tax_data = [
    {
        key: 'Net Income',
        values: [
                    {
                        "label" : "John Doe" ,
                        "value" : 1000000
                    } ,
                    {
                        "label" : "Jane Doe" ,
                        "value" : 1500000
                    } ,
                ]
            },
    {
        key: 'State and City Taxes',
        values: [
                    {
                        "label" : "John Doe" ,
                        "value" : 1000000 
                    } ,
                    {
                        "label" : "Jane Doe" ,
                        "value" : 1500000 
                    } ,
                ]
            },
    {
        key: 'Federal Taxes',
        values: [
                    {
                        "label" : "John Doe" ,
                        "value" : 1000000
                    } ,
                    {
                        "label" : "Jane Doe" ,
                        "value" : 1500000
                    } ,
                ]
            },
    {
        key: 'Total Due',
        values: [
                    {
                        "label" : "John Doe" ,
                        "value" : 1000000  
                    } ,
                    {
                        "label" : "Jane Doe" ,
                        "value" : 1500000 
                    } ,

                ]
            }
        ];
user1093111
  • 1,091
  • 5
  • 21
  • 47

1 Answers1

0

Something like this should work, though it's a rather strange way of arranging your data. Note that the construct $array[] = 1 is identical to (and more common than) array_push($array, 1). Also, do not use mysql_* functions.

$keys = array(
    "NetIncome"=>"Net Income",
    "State Tax"=>"State and City Taxes",
    "FedTax"=>"Federal Taxes",
    "TotalTax"=>"Total Due"
);

while($row = mysql_fetch_array($queryIndexData)) {
    foreach ($keys as $column=>$keys) {
        $a_json[$column][] = array(
            'label' => $row["person"],
            'value' => $row[$column] * 1,
        );
    }
}
foreach ($keys as $column=>$key) {
    $a_json_final[] = array(
        "key"=>$key,
        "values"=>$a_json[$column]
    );
}

// jQuery wants JSON data
echo json_encode($a_json_final);
Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Thanks for the advice and answer, why do you say it is stored in weird arrangement? – user1093111 Nov 21 '15 at 01:22
  • I would just expect that storing information with the person as a key would be more useful. Depends on your application though. – miken32 Nov 21 '15 at 01:46
  • Ah, ok. I'm new. I'm trying to see all the state taxes for a set of people. Could you do that if a person was the key? – user1093111 Nov 21 '15 at 02:03
  • Well I would be just asking the database for it and saving all this mucking around with arrays. `SELECT person, NetIncome FROM tablename`. I'd also be using a PDO connection which would dump the result set directly into an array. – miken32 Nov 21 '15 at 02:11