0

This is the static multidimension array and i converted to json.

$mailmsg = array(
  'type'=> 'line',
  'data' =>   array(
      'labels' => array("1","2","3","4","5"),  
      'datasets' => array(array("label"=>"A","fill"=>"false","yAxisID"=>"A","borderColor"=>"#bae755","data"=>array(100, 96, 84, 76, 69)),array("label"=>"B","fill"=>"false","yAxisID"=>"B","borderColor"=>"#55bae7","data"=>array(1, 1, 1, 1, 0)),array("label"=>"C","fill"=>"false","yAxisID"=>"C","borderColor"=>"#e755ba","data"=>array(5, 15, 10, 10, 0)))),
  ' '=>array("scales"=>array("yAxes"=>array(array("scaleLabel"=>array("display"=>"true","labelString"=>"Prn1"),"id"=>"A","type"=>"linear","position"=>"left"),array("scaleLabel"=>array("display"=>"true","labelString"=>"Prn2"),"id"=>"B","type"=>"linear","position"=>"left"),array("scaleLabel"=>array("display"=>"true","labelString"=>"Prn3"),"id"=>"C","type"=>"linear","position"=>"left")))
));
echo $json = json_encode((object)$mailmsg, JSON_NUMERIC_CHECK);

Result:

{
    "type": "line",
    "data": {
        "labels": ["1","2","3","4","5"],
        "datasets": [{
            "label": "A",
            "fill": "false",
            "yAxisID": "A",
            "borderColor": "#bae755",
            "data": [100, 96, 84, 76, 69]
        }, {
            "label": "B",
            "fill": "false",
            "yAxisID": "B",
            "borderColor": "#55bae7",
            "data": [1, 1, 1, 1, 0]
        }, {
            "label": "C",
            "fill": "false",
            "yAxisID": "C",
            "borderColor": "#e755ba",
            "data": [5, 15, 10, 10, 0]
        }]
    },
    "options": {
        "scales": {
            "yAxes": [{
                "scaleLabel": {
                    "display": "true",
                    "labelString": "Prn1"
                },
                "id": "A",
                "type": "linear",
                "position": "left"
            }, {
                "scaleLabel": {
                    "display": "true",
                    "labelString": "Prn2"
                },
                "id": "B",
                "type": "linear",
                "position": "left"
            }, {
                "scaleLabel": {
                    "display": "true",
                    "labelString": "Prn3"
                },
                "id": "C",
                "type": "linear",
                "position": "left"
            }]
        }
    }
}

I need to above static array to change dynamically using foreach.

3
  • 2
    Didn't understand the question what do you mean change dynamically Commented Aug 30, 2018 at 4:47
  • I have the data sets array separetely. I want to display the expected result by using the individual arrays Commented Aug 30, 2018 at 4:53
  • I still don't get it. Maybe you should just describe the results by giving examples, preferably examples that are much smaller than the above, unless that size is really necessary for something. Commented Aug 30, 2018 at 5:58

1 Answer 1

1

just pass the reference to the specific data in the array:

foreach($mailmsg['data']['datasets'] as $key => $value){

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.