0

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?

2
  • You question is too messy in the explanation part. I don't see the overall goal. Commented Dec 20, 2017 at 16:38
  • 1
    You could look for this type of structure, if you see fit: https://eval.in/922824. - Basically each variable will be a sub-array, keeping them together. Commented Dec 20, 2017 at 16:40

1 Answer 1

2

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

1 Comment

Forgot to mention that your variables should not need quotes

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.