Laravel:获取数据数组并使用For循环

时间:2019-06-18 06:34:36

标签: php arrays laravel for-loop

嗨,我正在尝试获取数据数组并使用For循环。所以我有一个叫做'color'的数组,我想用For循环数组。但是我在结果上有些麻烦。我已经尝试过更改数组并在foreach中使用,但是我不知道如何获得想要的结果。

结果:

"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.
                }
            ],

      }
  ]

我想要这样的答复:

 "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.
                    }
                ],
}
  ]

这是我的代码:

 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 个答案:

答案 0 :(得分:2)

$set是数组,不能分配为字符串。我的建议正在更改以下代码:

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


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

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

但是,如果您的$data_chart计数大于$color计数,则会返回错误,为了更好地处理,请使用以下代码:

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

        }