Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values lost with PHP multidimensional array

I got troubles with multidimensional array in PHP because I lost some values when I try to add other informations in my array.

Here is my initial code :

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

Here is the (correct and wanted) output of the var_dump($retour['07']['1']); : enter image description here


Then, I tried to store the value of 'OrderDate' in the $retour[$num_semaine][$num_journee] level.

Here is the new code :

$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']); :
enter image description here

Some values are now missing.


I tried it the other way around :

$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']); :
enter image description here

It looks like the second instructions erased all the things written with the first one.


Anyone got an idea on how I can make this right ?
Thanks to all of you who read till the end.

like image 483
pihug12 Avatar asked Feb 24 '26 02:02

pihug12


1 Answers

With this line....

$retour[$num_semaine][$num_journee] = array(
    'Date' => $donnees['OrderDate']
);

... you reassign a new array each time (to the given element). One obvious idea is to change it into this:

$retour[$num_semaine][$num_journee]['Date'] = $donnees['OrderDate'];

... so your code will be adjusting the array element (array itself) that exists already.

like image 83
raina77ow Avatar answered Feb 26 '26 16:02

raina77ow