I want to create a rank system for users on my website. The ranks will be decided by a number of factors like how long they've been a member, how many posts they created etc. Every data item is also divided by a "weight" determined by me, so that it is more representative of the actual user activity - I don't want 1 post be as significant as 1 day as a member. After the weighing all stats are added together to a total.
Then, I have to normalize the totals so that they are assigned to the ranks which range from 1 to 20, since some members have just a few points of activity and some veteran members have thousands of points. I do this by normalizing the data and scaling it down to the 1-20 rank range with this function:
function normalize($userTotal, $minOriginalRange, $maxOriginalRange, $minNewRange, $maxNewRange){
return $minNewRange + ((($maxNewRange - $minNewRange) * ($originalValue - $minOriginalRange)) / ($maxOriginalRange - $minOriginalRange));
}
This is usually called like so:
normalize(getUserTotal(), 0, getHighestTotalOfAllMembers(), 1, 20);
And so I got this as a result, key is rank and value is number of members who would get that rank:
Array
(
[1] => 7418
[2] => 1918
[3] => 289
[4] => 102
[5] => 62
[6] => 28
[7] => 21
[8] => 14
[9] => 1
[10] => 8
[11] => 6
[12] => 5
[13] => 1
[14] => 1
[17] => 1
[20] => 1
)
As you can see there are tons of users who are ranked low and very few who get assigned the mid and high ranks. I'd like to fix this by calculating the rank assigned using a logarithmic scale, so that it is easy to climb the ranks in the lower tiers and gets harder and harder the higher you go. That way it should spread out more evenly and more users will have ranks in the middle.
I don't know how to approach this however, I have never used logarithmic scales and always resorted to simple arithmetic in my code.
You would use a php math logarithm function and map it over the final array, for example:
function logfunction($v){
return round(log1p($v),2);
}
$simple_array = [7418, 1918, 289, 102, 62, 28, 21, 14, 1, 8, 6, 5, 1, 1, 1];
$logarithmic_array = array_map(logfunction, $simple_array);
print_r($logarithmic_array);
In the above, I use the log1p() function whichreturns log(1 + number) computed in a way that is accurate even when the value of number is close to zero (See: http://php.net/manual/en/function.log1p.php). Then I round the result to 2 decimal places for the sake of readability. The resulting $logarithmic_array output is:
Array
(
[0] => 8.91
[1] => 7.56
[2] => 5.67
[3] => 4.63
[4] => 4.14
[5] => 3.37
[6] => 3.09
[7] => 2.71
[8] => 0.69
[9] => 2.2
[10] => 1.95
[11] => 1.79
[12] => 0.69
[13] => 0.69
[14] => 0.69
)
I have an old windows 95 program that exports data without account numbers, seasonal accounts, and if accounts contains a sub account.
I am, however, able to print customer information and notes that has the above information to a pdf file and copy that text to notepad; which I would like to extract the data.
The order the data: 1) page headers (I do not need this data.)
Company Name
Customer Information and Notes
Computed Monday, August 10 2015 Page 1
2) standard titles and 3) the data after titles:
Ser Name: Block, Sunny Route: 1
Address: 3354 ASPEN RD. Frequency: Monthly
Address: ST PETE, GA 33333 Week/Day: First Monday
City State Zip: data Sched Time (HH:MM): 10:00A
Ser Phone: 555-1212 Service: BASIC SERVICE
Bill to: BLOCK,SUNNY Rate ($): 24.00
Company Name
Customer Information and Notes
Computed Monday, August 10 2015 Page 2
Address: 1123 Sligh Terms: CASH
Address: Apt B
notes: Sunny has a mean dog
Do not enter unless dog is put up
Then it loops to next customers data and so on.
The main titles never change, such as, ser name, route, address, notes, phone. There is a set number of titles in order; however, the title notes: can take 1 -16 lines; and the header is random throughout the data. and although the titles are in order, address is titled 4 times for both service- line 1 and line 2 and billing addresses- line 1 and line 2.
I would like to set variables to these titles and only take what's after them; the extraction part through PHP. Is there anyway to do this?
I don't think it's possible for a perfect solution, but FWIW, maybe this is good enough for you.
Without a known / reliable delimiter between clients, I can't think of any good way you can get the notes without having the header stuff for the next company included, unless you can do something involving a big lookup table of all client names.
I do have (an ugly) regex that may reliably help as far as the other stuff though:
$content='[the contents of your file]';
preg_match_all('~(Ser Name|Route|Address|Frequency|Week/Day|City State Zip|Sched Time \(HH:MM\)|Ser Phone|Service|Bill to|Rate \(\$\)|Terms|notes):\s*((?:(?!Ser Name|Route|Address|Frequency|Week/Day|City State Zip|Sched Time \(HH:MM\)|Ser Phone|Service|Bill to|Rate \(\$\)|Terms|notes).)+)~is',$content,$matches);
So this basically looks for the "header" and puts into first captured group, and then matches up to the next "header" and puts that into 2nd captured group.
Perhaps this is good enough for you, but TBH I can't think of anything better you can do, unless you can improve your extraction to a better format.
So your example data would output:
Array
(
[0] => Array
(
[0] => Ser Name: Block, Sunny
[1] => Route: 1
[2] => Address: 3354 ASPEN RD.
[3] => Frequency: Monthly
[4] => Address: ST PETE, GA 33333
[5] => Week/Day: First Monday
[6] => City State Zip: data
[7] => Sched Time (HH:MM): 10:00A
[8] => Ser Phone: 555-1212
[9] => Service: BASIC SERVICE
[10] => Bill to: BLOCK,SUNNY
[11] => Rate ($): 24.00
Company Name
Customer Information and Notes
Computed Monday, August 10 2015 Page 2
[12] => Address: 1123 Sligh
[13] => Terms: CASH
[14] => Address: Apt B
[15] => notes: Sunny has a mean dog
)
[1] => Array
(
[0] => Ser Name
[1] => Route
[2] => Address
[3] => Frequency
[4] => Address
[5] => Week/Day
[6] => City State Zip
[7] => Sched Time (HH:MM)
[8] => Ser Phone
[9] => Service
[10] => Bill to
[11] => Rate ($)
[12] => Address
[13] => Terms
[14] => Address
[15] => notes
)
[2] => Array
(
[0] => Block, Sunny
[1] => 1
[2] => 3354 ASPEN RD.
[3] => Monthly
[4] => ST PETE, GA 33333
[5] => First Monday
[6] => data
[7] => 10:00A
[8] => 555-1212
[9] => BASIC SERVICE
[10] => BLOCK,SUNNY
[11] => 24.00
Company Name
Customer Information and Notes
Computed Monday, August 10 2015 Page 2
[12] => 1123 Sligh
[13] => CASH
[14] => Apt B
[15] => Sunny has a mean dog
)
)
I am trying to calculate the winning order of golfers when they are tied in a competition.
These golf competitions are using the "stableford" points scoring system, where you score points per hole with the highest points winning. Compared to normal golf "stroke play" where the lowest score wins (though this also has the countback system, only calculating the lowest score in the event of a tie...)
The rules are to use a "countback". i.e., if scores are tied after 9 holes, the best placed of the ties is the best score from the last 8 holes. then 7 holes, etc.
The best I can come up with is 2 arrays.
An array with all the players who tied in a given round. ($ties)
One which has the full score data in (referencing the database playerid) for all 9 holes. ($tie_perhole)
I loop through array 1, pulling data from array 2 and using the following formula to create a temporary array with the highest score:
$max = array_keys($array,max($array));
If $max only has 1 item, this player is the highest scorer. the loop through the first array is "by reference", so on the next iteration of the loop, his playerid is now longer in the array, thus ignored. this continues until there is only 1 playerid left in the first array.
However, it only works if a single player wins in each iteration. The scenario that doesn't work is if a sub-set of players tie on any iterations / countbacks.
I think my problem is the current structure I have will need the original $ties array to become split, and then to continue to iterate through the split arrays in the same way...
As an example...
The $ties array is as follows:
Array
(
[18] => Array
(
[0] => 77
[1] => 79
[2] => 76
[3] => 78
)
)
The $tie_perhole (score data) array is as follows:
Array
(
[18] => Array
(
[77] => Array
(
[9] => 18
[8] => 16
[7] => 14
[6] => 12
[5] => 10
[4] => 8
[3] => 6
[2] => 4
[1] => 2
)
[79] => Array
(
[9] => 18
[8] => 17
[7] => 15
[6] => 14
[5] => 11
[4] => 9
[3] => 7
[2] => 5
[1] => 3
)
[76] => Array
(
[9] => 18
[8] => 16
[7] => 14
[6] => 12
[5] => 10
[4] => 8
[3] => 6
[2] => 4
[1] => 2
)
[78] => Array
(
[9] => 18
[8] => 17
[7] => 15
[6] => 13
[5] => 11
[4] => 9
[3] => 7
[2] => 5
[1] => 3
)
)
)
So in this competition, player's 78 and 79 score highest on the 8th hole countback (17pts), so 1st and 2nd should be between them. Player 79 should then be 1st on the 6th hole countback (14pts, compared to 13pts). The same should occur for 3rd and 4th place with the 2 remaining other players.
There are other scenarios that can occur here, in that within a competition, there will likely be many groups of players (of different amounts) on different tied points through the leaderboard.
Also note, there will be some players on the leaderboard who are NOT tied and stay in their current outright position.
The basics of the working code I have is:
foreach ($ties as $comparekey => &$compareval) {
$tie_loop = 0;
for ($m = 9; $m >= 1; $m--) {
$compare = array();
foreach ($compareval as $tie) {
$compare[$tie] = $tie_perhole[$comparekey][$tie][$m];
}
$row = array_keys($compare,max($compare));
if (count($row) == 1) {
$indexties = array_search($row[0], $ties[$comparekey]);
unset($ties[$comparekey][$indexties]);
// Now update this "winners" finishing position in a sorted array
// This is a multidimensional array too, with custom function...
$indexresults = searchForId($row[0], $comp_results_arr);
$comp_results_arr[$indexresults][position] = $tie_loop;
$tie_loop++;
}
// I think I need conditions here to filter if a subset of players tie
// Other than count($row) == 1
// And possibly splitting out into multiple $ties arrays for each thread...
if (empty($ties[$comparekey])) {
break;
}
}
}
usort($comp_results_arr, 'compare_posn_asc');
foreach($comp_results_arr as $row) {
//echo an HTML table...
}
Thanks in advance for any helpful insights, tips, thoughts, etc...
Robert Cathay asked for more scenarios. So here is another...
The leaderboard actually has more entrants (player 26 had a bad round...), but the code i need help with is only bothered about the ties within the leaderboard.
Summary leaderboard:
Points Player
21 48
21 75
20 73
20 1
13 26
This example produces a $tie_perhole array of:
Array
(
[21] => Array
(
[75] => Array
(
[9] => 21
[8] => 19
[7] => 16
[6] => 14
[5] => 12
[4] => 9
[3] => 7
[2] => 5
[1] => 3
)
[48] => Array
(
[9] => 21
[8] => 19
[7] => 16
[6] => 13
[5] => 11
[4] => 9
[3] => 8
[2] => 5
[1] => 3
)
)
[20] => Array
(
[73] => Array
(
[9] => 20
[8] => 18
[7] => 16
[6] => 13
[5] => 11
[4] => 8
[3] => 6
[2] => 5
[1] => 3
)
[1] => Array
(
[9] => 20
[8] => 17
[7] => 16
[6] => 14
[5] => 12
[4] => 9
[3] => 7
[2] => 4
[1] => 2
)
)
)
In this example, the array shows that players 75 and 48 scored 21 points that player 75 will eventually win on the 6th hole countback (14pts compared to 13pts) and player 48 is 2nd. In the next tied group, players 73 and 1 scored 20 points, and player 73 will win this group on the 8th hole countback and finishes 3rd (18 pts compared to 17 pts), with player 1 in 4th. player 26 is then 5th.
Note, the $tie_loop is added to another array to calculate the 1st to 5th place finishing positions, so that is working.
Hopefully that is enough to help.
Ok, so I don't understand golf at all... hahaha BUT! I think I got the gist of this problem, so heres my solution.
<?php
/**
* Author : Carlos Alaniz
* Email : Carlos.glvn1993#gmail.com
* Porpuse : Stackoverflow example
* Date : Aug/04/2015
**/
$golfers = [
"A" => [1,5,9,1,1,2,3,4,9],
"B" => [2,6,4,2,4,4,1,9,3],
"C" => [3,4,9,8,1,1,5,1,3],
"D" => [1,5,1,1,1,5,4,5,8]
];
//Iterate over scores.
function get_winners(&$golfers, $hole = 9){
$positions = array(); // The score numer is the key!
foreach ($golfers as $golfer=>$score ) { // Get key and value
$score_sub = array_slice($score,0,$hole); // Get the scores subset, first iteration is always all holes
$total_score = (string)array_sum($score_sub); // Get the key
if(!isset($positions[$total_score])){
$positions[$total_score] = array(); // Make array
}
$positions[$total_score][] = $golfer; // Add Golpher to score.
}
ksort($positions, SORT_NUMERIC); // Sort based on key, low -> high
return array(end($positions), key($positions)); // The last shall be first
}
//Recursion is Awsome
function getWinner(&$golfers, $hole = 9){
if ($hole == 0) return;
$winner = get_winners($golfers,$hole); // Get all ties, if any.
if(count($winner[0]) > 1){ // If theirs ties, filter again!
$sub_golfers =
array_intersect_key($golfers,
array_flip($winner[0])); // Only the Worthy Shall Pass.
$winner = getWinner($sub_golfers,$hole - 1); // And again...
}
return $winner; // We got a winner, unless they really tie...
}
echo "<pre>";
print_R(getWinner($golfers));
echo "</pre>";
Ok... Now ill explain my method...
Since we need to know the highest score and it might be ties, it makes no sense to me to maintain all that in separate arrays, instead I just reversed the
golfer => scores
to
Tota_score => golfers
That way when we can sort the array by key and obtain all the golfers with the highest score.
Now total_score is the total sum of a subset of the holes scores array. So... the first time this function runs, it will add all 9 holes, in this case theres 3 golfers that end up with the same score.
Array
(
[0] => Array
(
[0] => A
[1] => B
[2] => C
)
[1] => 35
)
Since the total count of golfers is not 1 and we are still in the 9th hole, we run this again, but this time only against those 3 golfers and the current hole - 1, so we are only adding up to the 8th hole this time.
Array
(
[0] => Array
(
[0] => B
[1] => C
)
[1] => 32
)
We had another tie.... this process will continue until we reach the final hole, or a winner.
Array
(
[0] => Array
(
[0] => C
)
[1] => 31
)
EDIT
<?php
/**
* Author : Carlos Alaniz
* Email : Carlos.glvn1993#gmail.com
* Porpuse : Stackoverflow example
**/
$golfers = [
"77" => [2,4,6,8,10,12,14,16,18],
"79" => [3,5,7,9,11,14,15,17,18],
"76" => [2,4,6,8,10,12,14,16,18],
"78" => [3,5,7,9,11,13,15,17,18]
];
//Iterate over scores.
function get_winners(&$golfers, $hole = 9){
$positions = array(); // The score numer is the key!
foreach ($golfers as $golfer => $score) { // Get key and value
//$score_sub = array_slice($score,0,$hole); // Get the scores subset, first iteration is always all holes
$total_score = (string)$score[$hole-1]; // Get the key
if(!isset($positions[$total_score])){
$positions[$total_score] = array(); // Make array
}
$positions[$total_score][] = $golfer; // Add Golpher to score.
}
ksort($positions, SORT_NUMERIC); // Sort based on key, low -> high
return [
"winner"=> end($positions),
"score" => key($positions),
"tiebreaker_hole" => [
"hole"=>$hole,
"score"=> key($positions)],
]; // The last shall be first
}
//Recursion is Awsome
function getWinner(&$golfers, $hole = 9){
if ($hole == 0) return;
$highest = get_winners($golfers,$hole); // Get all ties, if any.
$winner = $highest;
if(count($winner["winner"]) > 1){ // If theirs ties, filter again!
$sub_golfers =
array_intersect_key($golfers,
array_flip($winner["winner"])); // Only the Worthy Shall Pass.
$winner = getWinner($sub_golfers,$hole - 1); // And again...
}
$winner["score"] = $highest["score"];
return $winner; // We got a winner, unless they really tie...
}
echo "<pre>";
print_R(getWinner($golfers));
echo "</pre>";
Result:
Array
(
[winner] => Array
(
[0] => 79
)
[score] => 18
[tiebreaker_hole] => Array
(
[hole] => 6
[score] => 14
)
)
Array
(
[0] => LinkedIn Corporation
[1] => www.linkedin.com
[2] => 2029 Stierlin Ct
Mountain View, CA 94043-4655
United States map
[3] => +1.650.687.3600
[4] => Software & Internet, E-commerce and Internet Businesses
Software & Internet, Data Analytics, Management and Storage
Business Services, HR and Recruiting Services
[5] => 1K - 10K
[6] => > $1B
[7] => Publicly Traded - NASDAQ : LNKD
)
By the way foreach is not a loop but a language construct.
Traverse like this.
foreach($yourarr as $k=>$v)
{
echo "The Key:$k and The Value:$v<br>";
}
i use imdb api
$homepage = file_get_contents('http://www.imdbapi.com/?i='.$imdbID);
$arr = json_decode($homepage);
In $arr i have all data about related movie.
Maguire [Year] => 1996 [Rated] => R [Released] => 13 Dec 1996 [Genre] => Comedy, Drama, Romance, Sport [Director] => Cameron Crowe [Writer] => Cameron Crowe [Actors] => Tom Cruise, Cuba Gooding Jr., Renée Zellweger, Kelly Preston [Plot] => When a sports agent has a moral epiphany and is fired for expressing it, he decides to put his new philosophy to the test as an independent with the only athlete who stays with him. [Poster] => http://ia.media-imdb.com/images/M/MV5BMTkxNjc2NjQwOF5BMl5BanBnXkFtZTcwMDE2NDU2MQ##._V1_SX320.jpg [Runtime] => 2 hrs 19 mins [Rating] => 7.2 [Votes] => 97329 [ID] => tt0116695 [Response] => True )
what i want is that reaching specified key's value.
Is there a function like that? For example , i will give the key=> Actors and get all actors?
:/ Like this?
$homepage = file_get_contents('http://www.imdbapi.com/?i='.$imdbID);
$arr = json_decode($homepage, true);
print($arr['Actors']);