如何在Laravel中使用数组推送

时间:2019-06-17 12:57:34

标签: php arrays sql-server laravel

我正在尝试使用数组推送在Laravel数组中添加新项目,但是遇到了一些麻烦。我想在“ data_chart”中添加名为“ Color”的新项目。我已经尝试过使用数组Push和数组Unshift。有更好的方法吗?

我想要这样的结果:

"data": [
     {
       "id": 29,
       "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": {
           "data": [
            {
               "name": "Administrator",
               "total": "100",
               "color" "#9c1d1d" //I want color should be here in this line
             },
             {
                "name": "Staff",
                "total": "100",
                "color" "#9c1d1d" //new item named Color
              },
            ],
       }
     }
    ]

但是当我尝试使用数组推送时,结果将如下所示:

"data": [
         {
           "id": 29,
           "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": {
              "data": [
                {
                   "name": "Administrator",
                   "total": "100"
                    //color should be in here
                 },
                 {
                    "name": "Staff",
                    "total": "100"
                    //color should be in here
                  }
                ],
              "color": "#9c1d1d" //idk but the color shouldn't like this
           }
         }
        ]

这是我的代码:

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

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

            if (empty($key->store_procedure)) {
                $key->function_name = "";
                $data_chart = "";
            }else{
                try {
                    //data chart
                    $data_chart = DB::select($key->function_name); //call store procedure from SQL Server
                } catch (SqlException  $sqlEx) {
                    return response()->default(400, $sqlEx->getMessage());
                }
            }

            $color = "#9c1d1d"; //color value
            array_push($data_chart, $color); //here's the code

            $data[] = [
                'id' => $key->id,
                'title' => $key->title,
                'function_name' => $key->function_name,
                'function_drop' => $key->function_drop,
                'type_of_chart' => $key->type_of_chart,
                'embed' => $key->embed,
                'created_at' => $key->created_at,
                'updated_at' => $key->updated_at, 
                'data_chart' => $data_chart, //call data_chart
            ];  
            }
            return response()->default(200, trans('messages.success.get-data'),$data);
        } catch (Exception $e) {
            return response()->default(400, $e->getMessage());
        }
    }

2 个答案:

答案 0 :(得分:1)

您正在实际要推送的数组之外使用数组推送。

您的success: function (data) { // log data to the console so we can see console.log(data); $('.loadingSignin').toggleClass('animate-conceal').toggleClass('animate-reveal'); 数组有一个名为data_chart的嵌套数组,因此您可以像在数组周围循环:

data

构建foreach($data['data_chart'] as $row){ $row->color = "#9c1d1d"; // you are inside the nested data array } 数组并想在其中添加多余的项(颜色)时,可以使用上述循环。

答案 1 :(得分:1)

Laravel为我们提供了数组帮助器函数,通过这些函数,我们可以无循环地完成这些工作。

data_set($data_chart, 'data.*.color', $color);

有关与数组相关的更多辅助功能click here