0

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

0

2 Answers 2

1

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
// ...
Sign up to request clarification or add additional context in comments.

1 Comment

can you tell me how i'd put my array in this array? I can't do it with my array_push :(
0

Use

${$dayZ}[0][0]

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

2 Comments

says Notice: Undefined variable: d in C:\xampp\htdocs\index.php on line 130 -- dunno where this "d" is comming from :/
${$dayZ}[0][0] -- got it!!! Thanks :) you're the best! - I'll accept answer asap

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.