Page 1 of 1

Multidimenstional Arrays

Posted: Wed May 07, 2008 10:26 am
by Todlerone
Hello everyone, hope all is well or getting better at least. Thank-you in advance for any help on this post. I have a question regarding reordering multidimensional arrays. A have a two-dimensional array for my baseball team that is populated from a MySQL database with, wins, losses,ties, etc,etc. After all of the scores are processed and the while loop is done I then calculate a winning percentage for each team based on their wins and losses. It adds it to the [8] column of the array. I then use

Code: Select all

function reverse_compare($x,$y)
{
if ($x[8]==$y[8])
return 0;
elseif ($x[8]>$y[8]){
return -1;
else
return 1;
}
usort ($standings,'reverse_compare');
 
I have also created another multidimensional array to keep track of each teams head to head record (based on wins only). I created this array to help me deal with equal winning percentages, by this I mean, if two teams share an equal winning percentage I will evaluate their head to head records and if one of them has won more games against the other then I will place them higher in the standings. So my question is. How do you go about swapping just to rows of a multidimensional array only.

Cheers

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 10:32 am
by pickle
Not sure what you mean by swapping?

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 10:38 am
by Todlerone
Sorry. I simple print the array at the end after all sorting is done. I have no trouble reordering the entire array based on the winning percentage. I'm just not sure how to swap individual rows of an array. Would I just use the usort again and compare one element of one multidimensional array to one element of another multidimensional array? Or I'm I in left field on this one...pardon the pun.

Cheers

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 10:50 am
by pickle
Your call to usort() & it's subsequent calls to reverse_compare() do the swapping for you. Are you wanting to modify your reverse_compare() function so that in cases where it returns 0, it checks your second array to break the tie?

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 11:24 am
by Todlerone
pickle wrote:Your call to usort() & it's subsequent calls to reverse_compare() do the swapping for you. Are you wanting to modify your reverse_compare() function so that in cases where it returns 0, it checks your second array to break the tie?
Yes.

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 11:33 am
by pickle
You could make your second array a global. You could then refer to it in your function in that first if() condition.

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 11:43 am
by Todlerone
pickle wrote:You could make your second array a global. You could then refer to it in your function in that first if() condition.
Any pointers in what you mean. I'm a noob. Thanks pickle.

Re: Multidimenstional Arrays

Posted: Wed May 07, 2008 12:08 pm
by pickle

Re: Multidimenstional Arrays

Posted: Thu May 08, 2008 8:11 am
by Todlerone
Still not getting this one. Could anybody help again please and thank-you.

Re: Multidimenstional Arrays

Posted: Thu May 08, 2008 9:39 am
by pickle
Did you read the examples on that page? They explain pretty well how to reference a global variable.

Re: Multidimenstional Arrays

Posted: Thu May 15, 2008 9:24 am
by Todlerone
pickle wrote:Did you read the examples on that page? They explain pretty well how to reference a global variable.
Sorry for the late reply pickle, the baseball season started so I had to make sure some form of a standings was displayed (even if it isn't sorted yet-oh and I have three kids). I have tried the global (as described on these pages). My problem with this now is how to add a comparison loop into this function. I'm only interested in the $x==$y part. So if there equal, go into another loop which compares a specific array that I have created to keep tract of each teams wins against each other. My problem is, in this function I'm not sure of the first array value it's at. $standings [0][8] through to $standings [7][8] are my teams. My best guess is that if I place my new comparison loop they will cycle equally?

Code: Select all

 
// sort $standings in win% $standings[$x][8]
function reverse_compare($x,$y)
    {
    global $overall;
    if ($x[8]==$y[8])//column [8] is winning% in array
        if ($overall[$x][$y]>$overall[$y][$x]){
            return -1;
        }elseif ($overall[$x][$y]<$overall[$y][$x]){
            return 1;
        }else{
            return 0;
        }
    elseif ($x[8]>$y[8])//x>y gives descending values or x<y for ascending
        return -1;
    else
        return 1;
    }
 usort ($standings,'reverse_compare');
 
Like this?

Re: Multidimenstional Arrays

Posted: Thu May 15, 2008 9:42 am
by pickle
You have to declare $overall as global, at the beginning of the function.

Re: Multidimenstional Arrays

Posted: Thu May 15, 2008 9:46 am
by Todlerone
pickle wrote:You have to declare $overall as global, at the beginning of the function.
I have done the global in the beginning part of this file, should I do it inside the function only? Sorry to not quite understand the placement of the global declaration. I have also placed the global $overall in the other file that populates the $overall array, is that needed there also.

CHEERS

Re: Multidimenstional Arrays

Posted: Thu May 15, 2008 9:58 am
by pickle
As the documentation states, the global keyword tells PHP which scope you're referring to. Inside a function, you don't have access to variables outside the function's scope, unless they are globals or constants. You need to use the global keyword inside the function as well, so PHP knows which scope to look in.