PHP将arraylist添加到现有数组

时间:2016-11-30 12:43:15

标签: php json

我正在尝试以下面的JSON格式对数据进行编码。无法在类别中添加类别数组。长时间挠头......有谁可以帮我这个?

期望的输出:

{
"chart": {
                "caption": "Product-wise Quarterly Revenue vs. Profit %",
                "subCaption": "Harry's SuperMart - Last Year",
                "xAxisname": "Quarter",
                "pYAxisName": "Sales",
                "sYAxisName": "Profit %",
                "numberPrefix": "$",
                "sNumberSuffix": "%",
                "sYAxisMaxValue": "25",
                "paletteColors": "#0075c2,#1aaf5d,#f2c500",
                "bgColor": "#ffffff",
                "borderAlpha": "20",               
                "showCanvasBorder": "0",
                "usePlotGradientColor": "0",
                "plotBorderAlpha": "10",
                "legendBorderAlpha": "0",
                "legendShadow": "0",
                "legendBgAlpha": "0",
                "valueFontColor": "#ffffff",               
                "showXAxisLine": "1",
                "xAxisLineColor": "#999999",
                "divlineColor": "#999999",               
                "divLineIsDashed": "1",
                "showAlternateHGridColor": "0",
                "subcaptionFontBold": "0",
                "subcaptionFontSize": "14",
                "showHoverEffect": "1"
            },
            "categories": [
                {
                    "category": [
                        {
                            "label": "Q1"
                        },
                        {
                            "label": "Q2"
                        },
                        {
                            "label": "Q3"
                        },
                        {
                            "label": "Q4"
                        }
                    ]
                }
            ]
         }

我的PHP代码:

 $arrData = array(
                    "chart" => array(
                                    "caption"=> "Product-wise Quarterly Revenue vs. Profit %",
                "subCaption"=> "Harry's SuperMart - Last Year",
                "xAxisname"=> "Quarter",
                "pYAxisName"=> "Sales",
                "sYAxisName"=> "Profit %",
                "numberPrefix"=> "$",
                "sNumberSuffix"=> "%",
                "sYAxisMaxValue"=> "25",
                "paletteColors"=> "#0075c2,#1aaf5d,#f2c500",
                "bgColor"=> "#ffffff",
                "borderAlpha"=> "20",
                "showCanvasBorder"=> "0",
                "usePlotGradientColor"=> "0",
                "plotBorderAlpha"=> "10",
                "legendBorderAlpha"=> "0",
                "legendShadow"=> "0",
                "legendBgAlpha"=> "0",
                "valueFontColor"=> "#ffffff",
                "showXAxisLine"=> "1",
                "xAxisLineColor"=> "#999999",
                "divlineColor"=> "#999999",
                "divLineIsDashed"=> "1",
                "showAlternateHGridColor"=> "0",
                "subcaptionFontBold"=> "0",
                "subcaptionFontSize"=> "14",
                "showHoverEffect"=> "1"
        )
                );


                $arrData["categories"] = array();
                $arrData["category"] = array();
                //array_push($arrData["categories"],array( $arrData["category"]));
                //array_push($arrData["categories"],array( $arrData["category"]));
                array_push($arrData["category"], array(
                "label" => "Q1",
 ));


                array_push($arrData["category"], array(
                "label" => "Q2",
                ));
                 $json_string = json_encode($arrData, JSON_PRETTY_PRINT);
                 echo $json_string;

        }

PHP输出:

{
    "chart": {
        "caption": "Product-wise Quarterly Revenue vs. Profit %",
        "subCaption": "Harry's SuperMart - Last Year",
        "xAxisname": "Quarter",
        "pYAxisName": "Sales",
        "sYAxisName": "Profit %",
        "numberPrefix": "$",
        "sNumberSuffix": "%",
        "sYAxisMaxValue": "25",
        "paletteColors": "#0075c2,#1aaf5d,#f2c500",
        "bgColor": "#ffffff",
        "borderAlpha": "20",
        "showCanvasBorder": "0",
        "usePlotGradientColor": "0",
        "plotBorderAlpha": "10",
        "legendBorderAlpha": "0",
        "legendShadow": "0",
        "legendBgAlpha": "0",
        "valueFontColor": "#ffffff",
        "showXAxisLine": "1",
        "xAxisLineColor": "#999999",
        "divlineColor": "#999999",
        "divLineIsDashed": "1",
        "showAlternateHGridColor": "0",
        "subcaptionFontBold": "0",
        "subcaptionFontSize": "14",
        "showHoverEffect": "1"
    },
    "categories": [

    ],
    "category": [
        {
            "label": "Q1"
        },
        {
            "label": "Q2"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

for ($i = 1; $i <= 4; $i++) {
  $arrData['categories'][0]['category'] []= ["label" => "Q{$i}"];
}

顺便说一句,你应该使用[]=而不是array_push,因为这样就没有调用函数的开销。