要更改的PHP数组结构
问题描述:
I'm in a foreach loop and i'm stuck with something...
I have this array:
if($nr_programare == 1) {
$programare_start_old = strtotime('06:00');
$programare_end_old_stamp = $programare_start_old+($durata*3600);
$programare_end_old = date('H:i', $programare_end_old_stamp);
$data['nr'] = $nr_programare;
$data['sortiment'] = $sortiment;
$data['cantitate'] = $cantitate;
$data['start'] = '06:00';
$data['end'] = $programare_end_old;
$data['realizat'] = $cantitate_realizata;
} else {
end($data);
$programare_start_new = prev($data);
$programare_start_new_stamp = strtotime($programare_start_new);
$programare_end_new_stamp = $programare_start_new_stamp+($durata*3600);
$programare_end_new = date('H:i', $programare_end_new_stamp);
$data['nr'] = $nr_programare;
$data['sortiment'] = $sortiment;
$data['cantitate'] = $cantitate;
$data['start'] = $programare_start_new;
$data['end'] = $programare_end_new;
$data['realizat'] = $cantitate_realizata;
}
And I would like to transpose it to this structure of array:
array_push($data, array($nr_programare, array($sortiment, $cantitate, $durata, $programare_start_new, $programare_end_old, $cantitate_realizata), $today.' '.$programare_start_new, $today.' '.$programare_end_old));
It has to look like this in the end:
Array
(
[0] => Array
(
[0] => $nr_programare
[1] => Array
(
[0] => $sortiment
[1] => $cantitate
[2] => $durata
[3] => $programare_start_new
[4] => $programare_end_new
[5] => $cantitate_realizata
)
[2] => 11.03.2015 06:00
[3] => 11.03.2015 10:00
)
[1] => Array
(
[0] => $nr_programare
[1] => Array
(
[0] => $sortiment
[1] => $cantitate
[2] => $durata
[3] => $programare_start_new
[4] => $programare_end_new
[5] => $cantitate_realizata
)
[2] => 11.03.2015 06:00
[3] => 11.03.2015 10:00
)
[2] => Array
(
[0] => $nr_programare
[1] => Array
(
[0] => $sortiment
[1] => $cantitate
[2] => $durata
[3] => $programare_start_new
[4] => $programare_end_new
[5] => $cantitate_realizata
)
[2] => 11.03.2015 06:00
[3] => 11.03.2015 10:00
)
)
And so on...What do I need to do?
答
$arr=array ()
$last='';
foreach (....) {
if($nr_programare == 1) {
($arr[]= array(
[0] => $nr_programare
[1] => Array
(
[0] => $sortiment
[1] => $cantitate
[2] => $durata
[3] => $programare_start_new
[4] => $programare_end_new
[5] => $cantitate_realizata
)
[2] => 11.03.2015 06:00
[3] => 11.03.2015 10:00
)
)
}else {
($arr[]= array(
[0] => $nr_programare
[1] => Array
(
[0] => $sortiment
[1] => $cantitate
[2] => $durata
[3] => $last
[4] => $programare_end_new
[5] => $cantitate_realizata
)
[2] => 11.03.2015 06:00
[3] => 11.03.2015 10:00
)
)
$last=$programare_end_new;
}
}
Then $arr
will contain the array you are looking for
$last will change to the last $programare_end_new after each loop (where nr_programare != 1). You will have to change $last if you don't want it to be blank in the case that the first iteration is !=1