【问题标题】:Laravel : get data array and loop with ForLaravel:获取数据数组并使用 For 循环
【发布时间】:2019-10-31 17:40:47
【问题描述】:

您好,我正在尝试获取数据数组并使用 For 循环。所以我有一个名为“颜色”的数组,我想用 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());
        }
    }

【问题讨论】:

    标签: php arrays laravel for-loop


    【解决方案1】:

    $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;
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 1970-01-01
      • 2015-03-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多