在我的 sql 查询之后,使用 PDO 和 fetch() 函数,我想填充一个二维数组,然后我可以在我的网站中显示或导出。
我在这个过程中遇到了一个奇怪的问题:前两行在他们自己的子阵列中填满,但第三行没有,并继续填满第二个子阵列。
我想实现这一点:
$tableau_pour_lexport= array (
0 => "filename",
1 => array (colomn name1 .. colomn name n )
2=> array (value 1... value n)
3 => array (value 1 .. value n)
.... );
但我得到这样的东西:
$tableau_pour_lexport= array (
0 => "filename",
1 => array (colomn name1 .. colomn name n )
2=> array (value 1... value n)
3 => array (value 1 .. value n, value 1....value n, value 1... value n)
);
这是我的代码:
<?php
$sql_query="SELECT id_dossier, type_sortie, date_incident, no_train, commentaire, conclusion_cause, retard_min, etat, no_rame1 FROM sortie_prevue
UNION
SELECT id_dossier, type_sortie, date_incident, no_train, commentaire, etat, conclusion_cause, retard_min, no_rame1 FROM sortie_non_prevue
ORDER BY date_incident LIMIT 3";
$reponse = $bdd->query("$sql_query");
#文件名行
$tableau_pour_lexport[0]="titre du fichier";
#列名称行
$premiere_ligne= $reponse->fetch(PDO::FETCH_ASSOC);
foreach ($premiere_ligne as $cle=>$element1 )
{
$colonnes[]=$cle;
}
$tableau_pour_lexport[1]=$colonnes;
#value 而应该填满其余部分
while ($donnees = $reponse->fetch(PDO::FETCH_NUM))
{
foreach ($donnees as $valeur )
{
$ligne[]=$valeur;
}
$tableau_pour_lexport[]=$ligne;
}
print_r($tableau_pour_lexport);
$reponse->closeCursor(); // Termine le traitement de la requête
?>
欢迎任何帮助!如果有更聪明的方法来处理我的 sql 数据,有什么建议吗?