I'm trying to create a array()
from other arrays using foreach
. I have 3 arrays, and these arrays have values that repeat themselves and I want to create only 1 array with the values I need based on these arrays.
I need to create this to return in a JSON to consume in an application, so I need to identify the values in the arrays.
The end result I need is similar to that.
MATERIA | 1Bim | 2Bim | 3Bim | 4Bim
ARTE | 5.0 | 10.0 | 8.0 | 9.0
The arrays are like this
Array
(
[0] => Array
(
[ALU_NOME] => FERNANDO PAIVA
[M_DESCRICAO] => ARTE
[PE_DESCRICAO] => 1 BIMESTRE
[NT_NOTAFINAL] => 5.0
)
[1] => Array
(
(
[ALU_NOME] => FERNANDO PAIVA
[M_DESCRICAO] => ARTE
[PE_DESCRICAO] => 2 BIMESTRE
[NT_NOTAFINAL] => 10.0
)
)
[2] => Array
(
(
[ALU_NOME] => FERNANDO PAIVA
[M_DESCRICAO] => ARTE
[PE_DESCRICAO] => 3 BIMESTRE
[NT_NOTAFINAL] => 8.0
)
)
)
I'm trying to do it this way.
$retorno = array();
$materias = array();
$notas = array();
foreach($lista as $materia){
//adiciona a materia apenas 1x
if(!in_array($materia["M_DESCRICAO"], $materias)){
$materias[$key["M_DESCRICAO"]] = utf8_encode($materia["M_DESCRICAO"]);
}
//notas por bimestre para materia
if($materia["PE_DESCRICAO"] === "1 BIMESTRE"){
$notas["NOTA_1"] = $materia["NT_NOTAFINAL"];
}else if($materia["PE_DESCRICAO"] === "2 BIMESTRE"){
$notas["NOTA_2"] = $materia["NT_NOTAFINAL"];
}else if($materia["PE_DESCRICAO"] === "3 BIMESTRE"){
$notas["NOTA_3"] = $materia["NT_NOTAFINAL"];
}else if($materia["PE_DESCRICAO"] === "4 BIMESTRE"){
$notas["NOTA_4"] = $materia["NT_NOTAFINAL"];
}
//adiciona as notas a materia
array_push($materias, $notas);
}
//adiciona ano letivo ao array de retorno
$retorno[] = $lista[0]["M_ANOLETIVO"];
array_push($retorno, $materias);
print_r($retorno);
And the result of that is not even close to what I need.
<br />
<b>Notice</b>: Undefined variable: key in
<b>C:\xampp\htdocs\WS_Escola\controller\NotasController.php</b> on line
<b>30</b>
<br />
Array
(
[0] => 2015
[1] => Array
(
[] => ARTE
[0] => Array
(
[NOTA_1] => 10.00
)
[1] => Array
(
[NOTA_1] => 10.00
[NOTA_2] => 10.00
)
[2] => Array
(
[NOTA_1] => 10.00
[NOTA_2] => 10.00
[NOTA_3] => 10.00
)
)
)
Edit
I'm trying like this.
$notas = array();
$materia = '';
foreach($lista as $value){
if($value["M_DESCRICAO"] != $materia){
$materia = $value["M_DESCRICAO"];
$notas[$materia] = array();
}
$notas = array("Materia"=>$materia,
"Notas"=>array("Bimestre"=>$value["PE_DESCRICAO"],
"NotaFinal"=>$value["NT_NOTAFINAL"]));
//$notas[$materia][$value["PE_DESCRICAO"]] = $value["NT_NOTAFINAL"];
}
print_r($notas);
The result is accurate, but I am not able to associate the Arrays and I need to associate because I will use it as JSON.