尝试创建一个关联数组?

时间:2015-10-27 13:24:18

标签: php arrays json

我有一个数组

 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           
        )

)

现在我尝试创建一个关联数组来返回类似这样的JSON:

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

我不知道如何从我发布的这个数组中为这个JSON结果创建这个关联数组。

我尝试像这样创造,但我需要的结果不会返回

$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);

我尝试的结果是

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

如何创建这个关联数组以返回我需要的JSON?

修改

$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);

结果

{
  "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"
        }
      ]
    }
  ]
}

3 个答案:

答案 0 :(得分:1)

除了我的评论之外,只要M_DESCRICAO没有改变,这就是将所有笔记推送到数组的代码,因此需要首先排序起始数组$lista。这是你所追求的(代码未经过测试,办公室电脑: - ))?

$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);

答案 1 :(得分:1)

我制作了一个未经测试的(可能有一些错误,但在逻辑上它应该可以工作)代码段,但它可能对你有所帮助:

    $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);

这适用于多个M_DESCRICAO

答案 2 :(得分:1)

我已经重构了您的上一次编辑,以删除一些代码重复和复杂性。

$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);