简体   繁体   中英

Trying create an associative array?

I have an array

 Array
(
    [0] => Array
        (

            [NT_NOTAFINAL] => 10.00          
            [M_DESCRICAO] => ARTE          
            [PE_DESCRICAO] => 1 BIMESTRE           
        )

    [1] => Array
        (
            [NT_NOTAFINAL] => 10.00          
            [M_DESCRICAO] => ARTE          
            [PE_DESCRICAO] => 2 BIMESTRE           
        )

    [2] => Array
        (
            [NT_NOTAFINAL] => 10.00          
            [M_DESCRICAO] => ARTE          
            [PE_DESCRICAO] =>3 BIMESTRE           
        )

)

Now I'm trying create an associative array to return JSON something like this:

"Materia":[{"descricao":"ARTE", "Notas":["1 BIMESTRE":10.00, "2 BIMESTRE":10.00, "3 BIMESTRE":10.00]}]

I don't know how I could create this associative array to this JSON result from this array that I'm posting.

I'm trying create like this but the result what I need does not return

$notas = '';
$materia = '';
foreach($lista as $value){
    if($value["M_DESCRICAO"] != $materia){
        $materia = $value["M_DESCRICAO"];         
    } 
    $notas = array("Descricao"=>$materia, "Notas"=>array($value["PE_DESCRICAO"]=>$value["NT_NOTAFINAL"]));


}
$result = array("Materia"=>array($notas));
echo json_encode($result);

The result to what I'm trying is

{
  "Materia": [
    {
      "Descricao": "ARTE",
      "Notas": {
        "3 BIMESTRE": "10.00"
      }
    }
  ]
}

How could I create this associative array to return this JSON like I need ?

Edit

$notas = array();
$materia = '';
$materia_array = array();
foreach($lista as $value){
    if($materia == ''){
        $materia = $value["M_DESCRICAO"];
    }
    if($value["M_DESCRICAO"] != $materia){
        array_push($materia_array, (array("Descricao"=>$materia,"Notas"=>$notas)));
        $notas = array();
        $materia = $value["M_DESCRICAO"];
    }else{        
        array_push($notas, array($value["PE_DESCRICAO"]=>$value["NT_NOTAFINAL"]));
    }


}
$result = array("Materia"=>$materia_array);
echo json_encode($result);

Result

{
  "Materia": [
    {
      "Descricao": "ARTE",
      "Notas": [
        {
          "1 BIMESTRE": "10.00"
        },
        {
          "2 BIMESTRE": "10.00"
        },
        {
          "3 BIMESTRE": "10.00"
        }
      ]
    },
    {
      "Descricao": "C.SOCIAIS",
      "Notas": [
        {
          "2 BIMESTRE": "10.00"
        },
        {
          "3 BIMESTRE": "9.50"
        }
      ]
    },
    {
      "Descricao": "CIÊNCIAS E P. S.",
      "Notas": [
        {
          "2 BIMESTRE": "9.50"
        },
        {
          "3 BIMESTRE": "10.00"
        }
      ]
    }
  ]
}

In addition to my comment, this would be the code to push all notes to an array as long as the M_DESCRICAO does not change, so the starting array $lista needs to be ordered first. Is this what you were after (code is not tested, office computer :-))?

$notas = $materia = null;
$result = $tmp = array();
$len = count($lista);
for ($i=0;$i<$len;$i++) {
    $notas = array();
    $materia = $lista[$i]["M_DESCRICAO"];
    while (($materia == $lista[$i+1]["M_DESCRICAO"]) && ($i < ($len -1))) {
        $notas[$lista[$i]["PE_DESCRICAO"]] = $lista[$i]["NT_NOTAFINAL"];
        $i++;
    }
    // now $notas holds all corresponding entries
    $tmp[] = array("Descricao"=>$materia, "Notas" => $notas);
}
$result = array("Materia"=>array($tmp));
echo json_encode($result);

I worked out an untested (there might be some errors, but logically it should work) piece of code, but it might help you:

    $notas;
    $materia = '';
    $materia_array;
    foreach($lista as $value){
        if($materia == '')
            $materia = $value["M_DESCRICAO"];
        if($value["M_DESCRICAO"] != $materia){
            array_push($materia_array, (array("Descricao"=>$materia,"Notas"=>$notas));
             unset($notas); //<--------
             $notas = array();//<--------
            $materia = $value["M_DESCRICAO"];         
        }
        else
        {
            array_push($notas, array($value["PE_DESCRICAO"]=>$value["NT_NOTAFINAL"]))
        }

    }
    $result = array("Materia"=>$materia_array);
    echo json_encode($result);

This should work for multiple M_DESCRICAO values

I've refactored your last edit to remove some code duplication and complexity.

$result = array();

foreach ($lista as $value) {
    $materia = $value['M_DESCRICAO'];

    if (!isset($result[$materia])) {
        $result[$materia] = array(
            'Descricao' => $materia, 
            'Notas'     => array()
        );
    }

    $result[$materia]['Notas'][] = array(
        $value['PE_DESCRICAO'] => $value['NT_NOTAFINAL']
    );
}

$result = array('Materia' => array_values($result));

echo json_encode($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM