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

时间:2015-11-26 21:14:01

标签: php arrays

抱歉,如果我没有正确提问。

这是我表格的一部分:

enter image description here

daoExcel.php我有这个功能:

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;

            }

然后我将结果返回controllerExcel.pho并使用此函数中的数据:

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($列表);

以这种方式从数据库中打印所有数据:

enter image description here

好吧,我需要这个:

示例1:

我有这个:

enter image description here

但我只需要这个:

enter image description here

示例2:

我有这个:

enter image description here

我只需要这个:

enter image description here

示例3:

我有这个:

enter image description here

我只需要这个:

enter image description here

所以,我需要算上M / Ent'和' M / Sal'白天,我不需要[estado]

有关如何做的一些建议?

对不起我的英文。

1 个答案:

答案 0 :(得分:1)

您可以将结果存储在一个数组中,您可以将“fecha”用作数组键。

在你的foreach循环中你记得“estado”的值,这样你就知道要增加哪个字段然后从数组中取消它,因为你不需要它。

然后你可以通过“fecha”引用[{'phone': 'None', 'Name': 'Bala1'}, {'phone': 'None', 'Name': 'Bala'}] 数组并增加“count_sal”或“c​​ount_ent”

例如:

$result

将导致:

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

Demo with multiple dates