Been staring at this code for most of the afternoon and I'm beginning to be driven up the wall.

Basically I have an array like this:

Array
(
    [0] => -11
    [1] => -6
    [2] => -6
    [3] => -5
    [4] => -4
    [5] => -4
    [6] => -2
    [7] => -1
    [8] => -1
    [9] => -1
    [10] => -1
    [11] => 0
    [12] => 1
    [13] => 2
    [14] => 2
    [15] => 3
    [16] => 4
    [17] => 4
    [18] => 5
    [19] => 5
    [20] => 5
    [21] => 6
    [22] => 7
    [23] => 11
    [24] => 64
)

I want to be able to display a position like so

-11 => 1
-6 => 2
-6 => 2
-5 => 4
-4 => 5
-4 => 5
-2 => 7
-1 => 8
-1 => 8
-1 => 8 
-1 => 8
0 => 12
1 => 13
2 => 14
2 => 14
3 => 16
4 => 17
4 => 17
5 => 19
5 => 19
5 => 19
6 => 22
7 => 23
11 => 24
64 => 25

and so on.

Anyone got any ideas of an easy way to compute this?

    I'm not the fastest on pattern matching, so what rules are you using that says:

    -11 => 1

    -6 => 2
    -5 => 4

    To me, it looks like:

    index 0 => index 12
    index 1 => index 13
    index 3 => index 16

      It doesn't have to match anything in the array. I am wanting to compute a position based on score.

      The final positions should look like this:

      -11 => 1
      -6 => 2
      -6 => 2
      -5 => 4
      -4 => 5
      -4 => 5
      -2 => 7
      -1 => 8
      -1 => 8
      -1 => 8 
      -1 => 8
      0 => 12
      1 => 13
      2 => 14
      2 => 14
      3 => 16
      4 => 17
      4 => 17
      5 => 19
      5 => 19
      5 => 19
      6 => 22
      7 => 23
      11 => 24
      64 => 25
      

      Basically a value that matches an existing value, is assigned the same position. When a new value is found, the position assigned to this should be the position plus the total of matching values.

      I had thought about using array_count_values to do a calculation of the records and how many match but struggling to come up with a way to marry the two things together

        I don't know why, but I am having a heckuva time wrapping my brain around making the final output array itself, so...

        I am going to give you a function I wrote that will echo the results as you want them to appear, but as far as the array part is concerned, I am still working that one out in my head.

        $array = Array(-11, -6, -6, -5, -4, -4, -2, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 4, 5, 5, 5, 6, 7, 11, 64);
        function placement($array){
        	$counttotal = count($array);
        	for($i = 1; $i <= $counttotal; ){
        		echo $array[$i-1]." => ";
          	if((@$array[$i-1] != @$array[$i-2])){
            	echo $i."<br />";
          	} else {
            	echo ($i-1)."<br />";
        		}	
        	$i++;
        	}
        }
        
        placement($array);
        

        I hope this helps you a bit, and when I figure out how to make the output array, I will repost. (P.S. The output array will have to be multidimensional so as to not try to create duplicate indexes.)

          Thanks so much for looking at this, I've spent quite a while today in between other things looking at this and have struggled to get my head round it as well.

          You seem to be falling over where I was earlier.

          One idea I did have is to use array_count_values() which would return the unique values with a calculation like so:

          Array
          (
              [-11] => 1
              [-6] => 2
              [-5] => 1
              [-4] => 2
              [-2] => 1
              [-1] => 4
              [0] => 1
              [1] => 1
              [2] => 2
              [3] => 1
              [4] => 2
              [5] => 3
              [6] => 1
              [7] => 1
              [11] => 1
              [64] => 1
          )
          

          Perhaps this could be the key to it???

            OK, here it is. Like I said earlier, it needs to be a multidimensional array so as to maintain unique key(index) names. Since your output has non-unique keys and values, they need to be set one level deeper into the array.

            Let me know if you need help manipulating the final output array, but it should be pretty self explanatory.

            $array = Array(-11, -6, -6, -5, -4, -4, -2, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 4, 5, 5, 5, 6, 7, 11, 64);
            
            function placement($array){
            	global $finalarray;
            	$finalarray = array();
            	$counttotal = count($array);
            	for($i = 1; $i <= $counttotal; ){
            		//echo $array[$i-1]." => ";
              	if((@$array[$i-1] != @$array[$i-2])){
                	//echo $i."<br />";
                	$finalarray[] = array($array[$i-1] => $i);
              	} else {
                	//echo ($i-1)."<br />";
                	$finalarray[] = array($array[$i-1] => $i-1);
            		}
            	$i++;
            	}
            	return $finalarray;
            }
            
            print_r(placement($array));
            

            Outputs: Array ( [0] => Array ( [-11] => 1 ) [1] => Array ( [-6] => 2 ) [2] => Array ( [-6] => 2 ) [3] => Array ( [-5] => 4 ) [4] => Array ( [-4] => 5 ) [5] => Array ( [-4] => 5 ) [6] => Array ( [-2] => 7 ) [7] => Array ( [-1] => 8 ) [8] => Array ( [-1] => 8 ) [9] => Array ( [-1] => 9 ) [10] => Array ( [-1] => 10 ) [11] => Array ( [0] => 12 ) [12] => Array ( [1] => 13 ) [13] => Array ( [2] => 14 ) [14] => Array ( [2] => 14 ) [15] => Array ( [3] => 16 ) [16] => Array ( [4] => 17 ) [17] => Array ( [4] => 17 ) [18] => Array ( [5] => 19 ) [19] => Array ( [5] => 19 ) [20] => Array ( [5] => 20 ) [21] => Array ( [6] => 22 ) [22] => Array ( [7] => 23 ) [23] => Array ( [11] => 24 ) [24] => Array ( [64] => 25 ) )

              Write a Reply...