London O'Connell
London O'Connell

Reputation: 79

PHP Multidimensional Array with Variables

So I'm trying to make an array because I want it to rank it in order - but I have 3 variables I'd like to keep all together. Currently my Code is:

$WinningOrder = array("$ID1" => "$Score1", 
                      "$ID2" => "$Score2", 
                      "$ID3" => "$Score3",
                      "$ID4" => "$Score4", 
                      "$ID5" => "$Score5");

But I'd like to add another element to each which would be $Total1-$Total5

I thought of doing a multidimensional array but I'm unsure how to go about it when they're all variables.

What would be the easiest way to go about this?

Upvotes: 0

Views: 635

Answers (1)

logistef
logistef

Reputation: 796

this should work

$Score1 = 10;
$mainArray = array(
     array( "id"=> $ID1 , 
            "score" => $Score1 ,
            "total" => $Total1 
         ),
       array( "id"=> $ID2 , 
            "score" => $Score2 ,
            "total" => $Total2 
         )
    );

Access values like this

echo $mainArray[0]["score"]

this will output 10

You can also loop over the main array

foreach($mainArray as $item){
  print $item
}

Upvotes: 2

Related Questions