3

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It wasn't that obvious to me in the first place. :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.