2

Hi I'am trying to get data Array and looping with For. So I have array called 'color' and I want loop the Array with For. But I have some trouble in the result. I've been tried change the array and in foreach but I got no idea how to get result like what I want.

The Result :

"data": [
        {
            "title": "get data users",
            "function_name": "selectDataUser",
            "function_drop": "selectDataUser",
            "type_of_chart": "Pie",
            "embed": null,
            "created_at": "2019-06-15 03:26:09.000",
            "updated_at": null,
            "data_chart": [
                {
                    "name": "Administrator",
                    "total": "100",
                    "color": "0xFF888888" //color cannot be the same
                },
                {
                    "name": "Staff",
                    "total": "100",
                    "color": "0xFF888888" //the color must be different
                },
                {
                    "name": "Super Administrator",
                    "total": "1",
                    "color": "0xFF888888" //this is not result what I want.
                }
            ],

      }
  ]

I want response like this:

 "data": [
            {
                "title": "get data users",
                "function_name": "selectDataUser",
                "function_drop": "selectDataUser",
                "type_of_chart": "Pie",
                "embed": null,
                "created_at": "2019-06-15 03:26:09.000",
                "updated_at": null,
                "data_chart": [
                    {
                        "name": "Administrator",
                        "total": "100",
                        "color": "0xFF000000" //all this color different
                    },
                    {
                        "name": "Staff",
                        "total": "100",
                        "color": "0xFF444444" //the color different
                    },
                    {
                        "name": "Super Administrator",
                        "total": "1",
                        "color": "0xFF888888" //this is result what I want.
                    }
                ],
}
  ]

This my code :

 public function index(Request $request)
    {
        try {
            $query = $this->dashboard->with('rolesDashboard','userDashboard')
                                    ->orderBy('id')
                                    ->get();

            $data=[];
            foreach ($query as $key) {

            $data_chart = DB::select($key->function_name); //call store procedure from SQL Server

            $color = array(
                "0xFF000000" ,
                "0xFF444444" ,
                "0xFF888888" ,
                "0xFFCCCCCC",
                "0xFFFFFFFF",
                "0xFFFF0000" ,
                "0xFF00FF00" ,
            ); // this is array color

            for ($i=0; $i < count($data_chart) ; $i++) { 
                $set = $color[$i]; //Get data array color
            }


            foreach($data_chart as $row){
                $row->color = $set;
            }

            $data[] = [
                'title' => $key->title,
                'function_name' => $key->function_name,
                'created_at' => $key->created_at,
                'updated_at' => $key->updated_at, 
                'data_chart' => $data_chart, //return data_chart and color
            ];  
            }

            return response()->default(200, trans('messages.success.get-data'),$data);
        } catch (Exception $e) {
            return response()->default(400, $e->getMessage());
        }
    }

1 Answer 1

2

$set is array, cannot be assigned as a string. My advice is changing below code:

        for ($i=0; $i < count($data_chart) ; $i++) { 
            $set = $color[$i]; //Get data array color
        }


        foreach($data_chart as $row){
            $row->color = $set;
        }

to

        $i=0; 
        foreach($data_chart as $row){
            $row->color = $color[$i];
            $i++;
        }

but if your $data_chart count is more than $color count it will return error, for better handling use code below:

        $i=0; 
        foreach($data_chart as $row){
            if(isset($color[$i])){
            $row->color = $color[$i];
            $i++; }
            else{
            $i=0;
            }

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