猿问

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

我正在尝试获取数据数组并使用 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.

                    }

                ],

}

  ]


温温酱
浏览 258回答 1
1回答

MM们

$set是数组,不能作为字符串赋值。我的建议是更改以下代码:&nbsp; &nbsp; &nbsp; &nbsp; for ($i=0; $i < count($data_chart) ; $i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $set = $color[$i]; //Get data array color&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach($data_chart as $row){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row->color = $set;&nbsp; &nbsp; &nbsp; &nbsp; }到&nbsp; &nbsp; &nbsp; &nbsp; $i=0;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach($data_chart as $row){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row->color = $color[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; }但是如果您的$data_chart计数超过$color计数,它将返回错误,以便更好地处理使用以下代码:&nbsp; &nbsp; &nbsp; &nbsp; $i=0;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach($data_chart as $row){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($color[$i])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row->color = $color[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答