How to insert an array into another array in php

0

I can not perform this insertion even in an array, considering that I have this static array of example in php. Where I intend to visualize the record in a bar graph with the canvasjs library, where one of its attributes is to pass an array like the following:

$dataPoints = array(
                    array("label"=> "Matematicas", "y"=>22),
                    array("label"=> "Ingles", "y"=> 34),
                    array("label"=> "Fisica", "y"=> 4),
                    array("label"=> "Quimica", "y"=> 13),
                    array("label"=> "Arte", "y"=> 50)
                    );

This part is done in the controller (by the way I'm working with laravel) but now I want to get the data from a database. This is my method of my controller

 public function index()
    {
        $dataPoints= DB::table('materias')->get();
        return View('reporteMaterias',compact('dataPoints'));

    }

Now, if I pass the static array and charge it to the canvarjs in one of its parameters in this way it works correctly:

data: [{
              type: "column",
              showInLegend: true,
              legendMarkerColor: "grey",
              legendText: "Materias ",
              dataPoints: @json($dataPoints)
              }]
        });

So, going back to the main point, how would you make a dynamic array but query the database to be dynamic? if taking into account that this is my query $dataPoints= DB::table('materias')->get(); Now how do I pass it as the static array mentioned above?

    
asked by Luis Hernandez 04.12.2018 в 11:22
source

1 answer

0

You should indicate the fields of your table materias , but adding that there is a field nombre and another one called campo_y , it would be:

 public function index(){
        $d= DB::table('materias')->get();
        $dataPoints = array();
        foreach ($d as $registro) {
            $dataPoints[] = array('label'=>$registro['nombre'],'y'=>$registro['campo_y']);            
        }

        return View('reporteMaterias',compact('dataPoints'));
    };
    
answered by 04.12.2018 / 11:41
source