douxian5076
2015-11-26 21:14
浏览 71
已采纳

如何使用PHP将数据放在一个数组中?

sorry if I don't ask correctly.

This is part of my table:

enter image description here

In daoExcel.php I have this function:

public function selectInconsistencias(){

                    $aObjects=array();
                    $dbconn = new DBconnection();
                    $db = $dbconn->bdConnection();
                    $stmt = $db->prepare("SELECT rco_rut,rco_nombre,rco_marc, rco_estado FROM act_relcontrol");  

                    $stmt->execute();
                    $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    $result = $stmt->fetchAll();

                    foreach ($result as $row) {
                            $fecha = date("d/m/Y", strtotime(str_replace("/","-",$row['rco_marc']), mktime(0, 0, 0)));
                            $aTransfer = new DaoTransferExcel();
                            $aTransfer->setRut($row['rco_rut']);
                            $aTransfer->setNombre($row['rco_nombre']);
                            $aTransfer->setFecha($fecha);
                            $aTransfer->setEstado($row['rco_estado']);

                            $aObjects[]=$aTransfer;
                    }

                    return $aObjects;

            }

Then I return the results to controllerExcel.pho and I use the data in this function:

private function setDatosInconsistencias($datos){

        foreach($datos as $sKey=>$oValue){    
            $list[] = array('rut' => $oValue->getRut(),
                            'nombre' => $oValue->getNombre(),
                            'fecha' => $oValue->getFecha(),
                            'estado' => $oValue->getEstado(),
                            'count_ent' => '0',
                            'count_sal' => '0'
                            );
        }

        print_r($list);
        exit();
    }

print_r($list);

Prints all the data from data base of this way:

enter image description here

Well, I need this:

Example 1:

I have this:

enter image description here

But I need only this:

enter image description here

Example 2:

I have this:

enter image description here

And I need only this:

enter image description here

Example 3:

I have this:

enter image description here

And I need only this:

enter image description here

So, I need count the 'M/Ent' and 'M/Sal' by day and I don't need [estado].

Some advice of how do this?

Sorry by my english.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlu2935 2015-11-27 10:27
    已采纳

    You can store your results in an array where you can use "fecha" as an array key.

    In your foreach loop you remember the value of "estado" so you know which field to increment and then unset it from the array because you don't need it.

    Then you can reference the $result array by "fecha" and increment the "count_sal" or "count_ent"

    For example:

    $result = array();
    $list = array(
        22 => array(
            "rut" => "16.534.770-6",
            "nombre" => "Miguel Pichipillán S",
            "fecha" => "02/09/2015",
            "estado" => "M/Ent",
            "count_ent" => 0,
            "count_sal" => 0
        ),
        23 => array(
            "rut" => "16.534.770-6",
            "nombre" => "Miguel Pichipillán S",
            "fecha" => "02/09/2015",
            "estado" => "M/Ent",
            "count_ent" => 0,
            "count_sal" => 0
        ),
        24 => array(
            "rut" => "16.534.770-6",
            "nombre" => "Miguel Pichipillán S",
            "fecha" => "02/09/2015",
            "estado" => "M/Ent",
            "count_ent" => 0,
            "count_sal" => 0
        ),
        25 => array(
            "rut" => "16.534.770-6",
            "nombre" => "Miguel Pichipillán S",
            "fecha" => "02/09/2015",
            "estado" => "M/Sal",
            "count_ent" => 0,
            "count_sal" => 0
        ),
    );
    
    foreach ($list as $key => $value) {
        $estado = $value["estado"];
        $fecha = $value["fecha"];
    
        unset($value["estado"]);
    
        if (!isset($result[$fecha])) {
            $result[$fecha] = $value;
        }
    
        if ($estado === "M/Sal") {
            $result[$fecha]["count_sal"]++;
        }
    
        if ($estado === "M/Ent") {
            $result[$fecha]["count_ent"]++;
        }
    }
    
    var_dump($result);
    

    Will result in:

    array (size=1)
      '02/09/2015' => 
        array (size=5)
          'rut' => string '16.534.770-6' (length=12)
          'nombre' => string 'Miguel Pichipillán S' (length=21)
          'fecha' => string '02/09/2015' (length=10)
          'count_ent' => int 3
          'count_sal' => int 1
    

    Demo with multiple dates

    打赏 评论

相关推荐 更多相似问题