Jump to content

Arrays array[1] gets overwrote by next


Huijari

Recommended Posts

So I have a code:

 

//Example there are 2 levels with same number of plays (3 plays)
$array[$plays] = $level;
// at the end of code
krsort($array,SORT_NUMERIC);

 

If it now takes level with 3 plays, it adds it to array but when next level with same plays comes, it comes in array as it.

My code is to sort these levels so that level with most plays is first.

 

Here's what I mean:

$array[3] = level1;
// next time
$array[3] = level2;

 

I tried this:

 

if ($array[$plays]=="") {
$array[$plays] = $level;
} else {
$array[$plays+1] = $level;
}

But it won't work because if you have 3 levels with plays 3,3 and 4,

and level with 4 plays comes first to array

Then when comes 3, its ok

But then the next 3, it adds 1 to it, so its 4, but there is already array[4] so it won't work

Link to comment
https://forums.phpfreaks.com/topic/204781-arrays-array1-gets-overwrote-by-next/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.