构建一个多维json对象

时间:2015-11-20 18:47:51

标签: php json

我正在构建税务计算器,并且我已经能够为一个密钥构建一个JSON对象。我需要扩展它以增加3个对象,但我遇到了麻烦。

目前,我可以做净收入:

$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);

它不是很干净,我对密钥进行了硬编码。如何从$ netincome,$ statetax,$ fedtax和$ totaltax变量动态生成密钥?

所以我的JSON看起来像:

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 
                    } ,

                ]
            }
        ];

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效,尽管这是一种安排数据的奇怪方式。请注意,构造$array[] = 1array_push($array, 1)相同(并且更常见)$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); 。另外,do not use mysql_* functions

k