Laravel PHP return array in a specific way
I'm trying to return an array to json in laravel so i can build a chart on a view. The controller looks like this.
//Paso 1. Buscar el gr??fico asociado a este ID
$chartset = Chart::findOrFail($chart);
$q = $chartset->dataset()->first(); //Buscar la pregunta
$Question = Question::find($q->labels); //Para usar los datos en el arreglo que regresa en JSON
//Armando los Datos
//Hace falta asignar el tipo de gr??fico
//Paso 2 Crear el Arreglo para devolver el chart data
$ChartData = array( //Arreglo con los par??metros del Chart
"chart" => array(
"title" => $chartset->title,
"caption" => $Question->FullLabel,
"subcaption" => "February 2016",
"captionFontSize" => "24",
"paletteColors" => "#A2A5FC, #41CBE3, #EEDA54, #BB423F, #F35685",
"baseFont" => "Quicksand, sans-serif",
//more chart configuration options
)
);
$ChartData["data"] = array(); //Arreglo donde iran los labels
//Paso 3, Iterar por cada uno de los datos en el chart
foreach($chartset->dataset()->get() as $charts){
$ans = Answer::where('AnswerId',$charts->data) //Obtener Las respueestas
->where('QuestionId',$charts->labels)
->first();
$responses = DB::table('responses') //Obtener los respondientes que contestaron con esa respuesta
->join('questions', 'responses.QuestionId', '=', 'questions.QuestionId')
->select('questions.FullLabel','responses.QuestionId',DB::raw('responses.Value as Label'),DB::raw('count(responses.Value) as Total'))
->where('responses.QuestionId',$ans->QuestionId)
->where('responses.Value',$ans->Label)
->groupBy('questions.FullLabel','responses.Value','responses.QuestionId')
->get();
foreach($responses as $response){ //Llenar el arreglo con datos
array_push($ChartData["data"], array(
"label" => $response->Label,
"value" => $response->Total
));
}
}
return response()->json($ChartData);
And the returned value looks like this
{
"chart": {
"title": "Dashboard Test",
"caption": "Q062. Ya que vio el anuncio, ??c??mo se siente sobre TABCIN?",
"subcaption": "February 2016",
"captionFontSize": "24",
"paletteColors": "#A2A5FC, #41CBE3, #EEDA54, #BB423F, #F35685",
"baseFont": "Quicksand, sans-serif"
},
"data": [
{
"label": "-3 La odio",
"value": 5
},
{
"label": "+3 La amo",
"value": 127
},
{
"label": "-1",
"value": 15
},
{
"label": "-2",
"value": 6
},
{
"label": "0 Neutral",
"value": 214
},
{
"label": "+2",
"value": 150
},
{
"label": "+1",
"value": 85
}
]
}
What I want is the returned data to look like this.
{
"chart": {
"title": "Dashboard Test",
"caption": "Q062. Ya que vio el anuncio, ??c??mo se siente sobre TABCIN?",
"subcaption": "February 2016",
"captionFontSize": "24",
"paletteColors": "#A2A5FC, #41CBE3, #EEDA54, #BB423F, #F35685",
"baseFont": "Quicksand, sans-serif"
},
"data": [
{
"label": ["-3 La odio","+3 La amo","-1","-2","0 Neutral","+2","+1",],
"value": [5,127,15,6,214,150,86],
},
]
}
Is there any way to return the label and value data like that?.
Thanks in advance, any help will be appreciated.
Answer
Solution:
Simply change the way you build the data array like this
foreach($responses as $response){ //Llenar el arreglo con datos
$ChartData["data"]['label'][] = $response->Label;
$ChartData["data"]['value'][] = $response->Total;
}
Source