数组来自查询结果php里面的同时

时间:2012-12-13 13:29:03

标签: php arrays loops

我有这个代码,这是特殊的工作......我正在尝试这样做:

Array
(
    [servicio_id 1] => Array
        (
            [peticion_id 1] => Array
                (...)
            [peticion_id 2] => Array
                (...)
            [peticion_id 3] => Array
                (...)
         )
    [servicio_id 2] => Array
        (
            [peticion_id 1] => Array
                (...)
            [peticion_id 2] => Array
                (...)
            [peticion_id 3] => Array
                (...)
         )
)

所以每个[servicio_id]都有[peticion_id]有自己的值......问题是我的代码实际上在每个[peticion_id]数组中设置了相同的值,只要循环运行就增加它的大小。有关如何在[servicio_id]完成后清除并重新启动[peticion_id]数组的任何建议吗?

谢谢你的建议

while($row = sqlsrv_fetch_array($sqlQuery)) {
    if($row['peticion_id'] == 0) {
        $ok[]       =   round($row['valor'], 3);
        if($row['media_ok'] == null) {
            $medias_ok = array("0", "0", "0", "0");
        } else {
            $medias_ok = Umbrales::getValues($row['media_ok'], $row['media']);
        }   
        $max_ok[] = $medias_ok[1];
        $min_ok[] = $medias_ok[0];
    }
    if($row['peticion_id'] == 1) {
        $ko[]       =   round($row['valor'], 3);
        if($row['media_ok'] == null) {
            $medias_ko = array("0", "0", "0", "0");
        } else {
            $medias_ko = Umbrales::getValues($row['media_ok'], $row['media']);
        }   
        $max_ko[] = $medias_ko[2];
    }
    if($row['peticion_id'] == 2) {
        $rt[]       =   round($row['valor'], 3);
        if($row['media_ok'] == null) {
            $medias_rt = array("0", "0", "0", "0");
        } else {
            $medias_rt = Umbrales::getValues($row['media_ok'], $row['media']);
        }   
        $max_rt[] = $medias_rt[3];
    }
    $datos[$servicios[$row['servicio_id']]] = array(
    "OK" => $ok,
    "KO" => $ko,
    "RT" => $rt,
    "UMBRAL_MIN_OK" => $min_ok,
    "UMBRAL_MAX_OK" => $max_ok,
    "UMBRAL_MAX_KO" => $max_ko,
    "UMBRAL_MAX_RT" => $max_rt
    );
}

1 个答案:

答案 0 :(得分:0)

您需要清除每个循环上的数组。 在你读完之后它应该是:

while($row = sqlsrv_fetch_array($sqlQuery)) {

    $ok = array();
    $ko = array();
    $rt = array();
    $min_ok = array();
    $max_ok = array();
    $max_ko = array();
    $max_rt = array();

    if($row['peticion_id'] == 0) {