2016-11-30 146 views
-1

我試圖以低於JSON格式對數據進行編碼。無法在類別內添加類別數組。長時間抓我的頭..任何人都可以幫助我嗎?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" 
         }, 
         { 
          "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" 
     } 
    ] 
} 

回答

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

順便說一句,你應該使用[]=代替array_push,因爲...在這種情況下,調用函數沒有任何開銷。

+0

謝謝。我會試試這個 – ram