Question

I am very new to programming and I got a job from school to do a "forecast" weather map from random generated numbers.

I already got 7 different arrays for every day (day1 -> day7) and all the code afterwards to check the required stuff and create the weather map.

Now I have the problem that I'd like to go through all the 7 arrays in 1 for-loop. I asked a friend and I think it's a very tiny thing missing / done wrong...

As I already got all the code afterwards (currently hardcoded for 1 day) I'd be pleased if you could provide me a solution for my (I guess crappy) way, not a totally different way eventhough it's most likely much more efficent what so ever.

I tried it like this:

for($daynr = 1; $daynr < 8; $daynr++){
    $dayZ = 'day'.$daynr;
    print_r($$dayZ);
    ...

This works like a charm and gives out array 1-7. But when I try to access a part of dayZ I get stuck:

echo $$dayZ[0][0]

Gives out:

Notice: Undefined variable: in C:\xampp\htdocs\index.php on line 139

As many stuff later like $temp = explode('/',$$dayZ[$region_nr][3])... need to access the day-array it would be cool to have a solution for that.

Btw, something like echo $day1[0][0] works without problems.

Any help very appreciated, thanks in advance

Best regards

Michael

Était-ce utile?

La solution 2

Use

${$dayZ}[0][0]

Keep in mind that this kind of variable format does NOT improve readability or good code.

Autres conseils

A cleaner solution would be to place the days in another array. So instead of $day1, $day2 etc you have an array $days containing your arrays for each day.

$days[0][0][0] // monday
$days[1][0][0] // tuesday
// ...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top