我在PHP中遇到了多维数组的麻烦,因为当我尝试在我的数组中添加其他信息时,我丢失了一些值。
这是我的初始代码:
$reponse = $bdd->query("select OrderDate, NomChaine, NomJob, Debut, Fin from ReportJobs where NomJob in ('NPT01000', 'NPT11999') order by OrderDate ASC;");
while ($donnees = $reponse->fetch())
{
$num_semaine = date('W', strtotime($donnees['OrderDate']));
$num_journee = date('N', strtotime($donnees['OrderDate']));
$retour[$num_semaine][$num_journee][substr($donnees['NomChaine'], 0, -2)][$donnees['NomJob']] = array(
'Debut' => $donnees['Debut'],
'Fin' => $donnees['Fin']
);
}
$reponse->closeCursor();
var_dump($retour['07']['1']);
以下是var_dump($retour['07']['1']);
的(正确的和想要的)输出:
然后,我尝试将'OrderDate'的值存储在$retour[$num_semaine][$num_journee]
级别。
这是新代码:
$reponse = $bdd->query("select OrderDate, NomChaine, NomJob, Debut, Fin from ReportJobs where NomJob in ('NPT01000', 'NPT11999') order by OrderDate ASC;");
while ($donnees = $reponse->fetch())
{
$num_semaine = date('W', strtotime($donnees['OrderDate']));
$num_journee = date('N', strtotime($donnees['OrderDate']));
$retour[$num_semaine][$num_journee] = array(
'Date' => $donnees['OrderDate']
);
$retour[$num_semaine][$num_journee][substr($donnees['NomChaine'], 0, -2)][$donnees['NomJob']] = array(
'Debut' => $donnees['Debut'],
'Fin' => $donnees['Fin']
);
}
$reponse->closeCursor();
var_dump($retour['07']['1']);
var_dump($retour['07']['1']);
:
现在缺少一些值。
我反过来试过了:
$reponse = $bdd->query("select OrderDate, NomChaine, NomJob, Debut, Fin from ReportJobs where NomJob in ('NPT01000', 'NPT11999') order by OrderDate ASC;");
while ($donnees = $reponse->fetch())
{
$num_semaine = date('W', strtotime($donnees['OrderDate']));
$num_journee = date('N', strtotime($donnees['OrderDate']));
$retour[$num_semaine][$num_journee][substr($donnees['NomChaine'], 0, -2)][$donnees['NomJob']] = array(
'Debut' => $donnees['Debut'],
'Fin' => $donnees['Fin']
);
$retour[$num_semaine][$num_journee] = array(
'Date' => $donnees['OrderDate']
);
}
$reponse->closeCursor();
var_dump($retour['07']['1']);
var_dump($retour['07']['1']);
:
看起来第二条指令删除了用第一条指令写的所有内容。
任何人都知道如何才能做到这一点? 感谢所有读到最后的人。
答案 0 :(得分:2)
用这条线......
$retour[$num_semaine][$num_journee] = array(
'Date' => $donnees['OrderDate']
);
...每次重新分配一个新数组(到给定元素)。一个明显的想法是将其改为:
$retour[$num_semaine][$num_journee]['Date'] = $donnees['OrderDate'];
...所以你的代码将调整已存在的数组元素(数组本身)。