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

如何使用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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c