Create an associative array from another array with foreach?

4

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.

    
asked by anonymous 25.10.2015 / 08:52

1 answer

3

Maybe this will solve, make a foreach in the list and at each turn you check if it is the same student of the previous round, if not, then create a new position in the array with the name of the student to receive the array with the notes:

    $notas = array();
    $aluno = '';
    $materia = '';
    foreach ($lista as $i => $nota) {
        if($nota['ALU_NOME'] != $aluno){
            $aluno = $nota['ALU_NOME'];
            $notas[$aluno] = array();
        }
        if($nota['M_DESCRICAO'] != $materia || $nota['ALU_NOME'] != $aluno){
            $materia = $nota['M_DESCRICAO'];
            $notas[$aluno][$materia] = array();
        }
        $notas[$aluno][$materia][$nota['PE_DESCRICAO']] = $nota['NT_NOTAFINAL'];

    }
    var_dump($notas);

The result would be this:

array (size=2)
  'FERNANDO PAIVA' => 
    array (size=2)
      'ARTE' => 
        array (size=3)
          '1 BIMESTRE' => float 5.9
          '2 BIMESTRE' => float 10
          '3 BIMESTRE' => float 5
      'GEOGRAFIA' => 
        array (size=1)
          '1 BIMESTRE' => float 9.5
  'OUTRO ALUNO' => 
    array (size=1)
      'ARTE' => 
        array (size=1)
          '1 BIMESTRE' => float 7.1

I hope it solves.

    
25.10.2015 / 21:39