Help me with PHP multidimensional array?

cristianods

New member
The first problem that you have here is that you have a double key on the array, the S key. Change it to correct the array otherwise any code will fail.

BTW: All act keys should be string, so 'act1' and so on...

The code that you have wrote is almost correct, but here are some small tweaks..

<?php
foreach ($daily_acts as $day => $schedule)
{
switch($day)
{
case 'S':
echo 'Sunday <br />';
break;
// so on until saturday
default:
// do nothing
break;
}
foreach ($schedule as $act => $workType)
{
echo "{$act} {$workType} <br />";
}
}
?>

Now all you got to do is tweak it to work for you ;)
 
The first problem that you have here is that you have a double key on the array, the S key. Change it to correct the array otherwise any code will fail.

BTW: All act keys should be string, so 'act1' and so on...

The code that you have wrote is almost correct, but here are some small tweaks..

<?php
foreach ($daily_acts as $day => $schedule)
{
switch($day)
{
case 'S':
echo 'Sunday <br />';
break;
// so on until saturday
default:
// do nothing
break;
}
foreach ($schedule as $act => $workType)
{
echo "{$act} {$workType} <br />";
}
}
?>

Now all you got to do is tweak it to work for you ;)
 
<?php

$daily_acts = array("S" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout),
"M" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout),
"T" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout),
"W" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout),
"Th" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout),
"F" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout),
"S" =>array([act1] => Pushup,
[act2] => Roadwork,
[act3] => weightlifting,
[act4] => swimming,
[act5] => punchingbag,
[act6] => aerobics,
[act7] => bicepswork,
[act8] => bicycleexercise,
[act9] = > cardiacworkout));

//what i wanna do is to create a script that prints out the day of the week, act number and activity
//like this
// S [act1] Pushup
// S [act2] Roadwork... so on and so forth for each of the days of the week
//how do i go about doing that


// i have tried using the following code but the days of the week don't print out

foreach ($daily_acts as $show) {
foreach ($show as $key => $value){
echo "$key : $value <br />\n";
}
}
?>
 
Back
Top