Avatar of sai4
sai4
 asked on

4 dimensional associate array in PHP?

can someone give me an example of how to create a 4 dimensional associate array in PHP?  Thank you very much.

said
PHP

Avatar of undefined
Last Comment
sai4

8/22/2022 - Mon
Zyloch

Just do it as you would any multi-dimensional array.

$multi = array();
for ($i = 0; $i < 5; $i++) {
    $multi[$i] = array();
    for ($j = 0; $j < 5; $j++) {
        $multi[$i][$j] = array();
        for ($k = 0; $k < 5; $k++) {
            $multi[$i][$j][$k] = array();
            for ($l = 0; $l < 5; $l++) {
                $multi[$i][$j][$k][$l] = $i.$j.$k.$l;
            }
        }
    }
}

print_r($multi);
ASKER CERTIFIED SOLUTION
Zyloch

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Attabui

Man, that's a fun one to visualize.  Yay for hypercubes.  Out of curiosity, why do you need four dimensions?
sai4

ASKER
Thank you Zyloch very much.


Hi Attabui,

I'm a new teacher at a school, and yesterday they had the end of year "scheduling meeting."   The meeting consists of trying to figure out when teachers can teach their classes.  To solve the problem, one person went to the marker board and began making a table.  To me that seems nuts so I wanted to try and solve the system using "solver from excel" or an algoithm like simulated simulated annealing, or a genetic algorithm.  I'm interested in 4d arrays becasue what occurs on a period of a given day is constrained by subject, teacher and grade so I wanted to have an array like  Period[Teacher][subject][day][grade]= "period number."  Orignally I tried to download a trial copy of Matlab to do this but I wasn't able to so I figured if I'm going to use another language PHP would make it easy to post on the school intranet.  Honestly it's been so long since I've done any of this stuff, so if you have any suggestions please let me know.  After looking at this is seems a hash table would be better than an array.

thank you

Steve


I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck