Multidimenstional Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Multidimenstional Arrays

Post 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
Last edited by Todlerone on Wed May 07, 2008 10:34 am, edited 2 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post by pickle »

Not sure what you mean by swapping?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: Multidimenstional Arrays

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: Multidimenstional Arrays

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post by pickle »

You could make your second array a global. You could then refer to it in your function in that first if() condition.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: Multidimenstional Arrays

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: Multidimenstional Arrays

Post by Todlerone »

Still not getting this one. Could anybody help again please and thank-you.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post by pickle »

Did you read the examples on that page? They explain pretty well how to reference a global variable.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: Multidimenstional Arrays

Post 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?
Last edited by Todlerone on Thu May 15, 2008 10:53 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post by pickle »

You have to declare $overall as global, at the beginning of the function.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: Multidimenstional Arrays

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multidimenstional Arrays

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply