0

I'm trying to achieve a list of ordered score points a user received and the number of times he received each score using the array that comes from the database $user_rating_points. The base score values are defined as an array of possible points $score_points.

Imagining that the query for a user gives me the following array for his given points:

    // The base score points' scale
    $score_points = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    // The array of points a user received (from the database query)
    $user_rating_points = [1, 3, 2, 7, 3, 4, 9, 2, 10, 6, 1, 7, 10, 8, 4, 8, 9, 4, 7, 10, 5];

I want to achieve something like the following:

  • 1 point: 3 times
  • 2 points: 2 times
  • 3 points: 5 times
  • ...
  • 9 points: 2 times
  • 10 points: 3 times

I've tried using the array_count_values($user_rating_points) with sort($user_rating_points); but either on a HTML ul or a print_r($user_rating_points) I'm unable to get a list like the above example.

Thanks in advance for any help on this issue that is probably much simpler to solve than I expect, yet it seems like I've gone into a loop and not finding a solution.

7
  • The "what you want to achieve" suggests there are 3 of #1 in $user_rating_points yet I can see only 2. The same for #3 where I can see only 2. Is that just dummy data or some other criteria I've missed? Commented Jan 29, 2022 at 16:15
  • It means score 1 was given 3 times, score 2 was given 2 times, and son and so forth. Yes, it's dummy data not to pile the post with a bunch of real data. From here I presume I will be able to figure it out. Commented Jan 29, 2022 at 16:18
  • So you are saying that there ought to be 3 of #1 in $user_rating_points as it was awarded 3 times? The same for the other numbers? ie 10 points awarded 3 times so will appear 3 times in $user_rating_points? Commented Jan 29, 2022 at 16:23
  • Yes, correct. I wish to get the list of how many times the user got scored for each of the 1 to 10 score points. Scored 1 point 2 times; scored 2 points 33 times, scored 10 points 5 times, etc. Commented Jan 29, 2022 at 16:26
  • well that is what array_count_values will give you so I'm confused why you think otherwise. Looking at the arrays given there is a discrepancy with what you want and what the arrays contain. Are the what you want examples accurately based upon the arrays given here or just for example?? Commented Jan 29, 2022 at 16:32

2 Answers 2

2

Here is my solution. Keys are sorted 1->10, no 0. If you have points which appear 0 times, they will also be present in the results.

<?php
// The base score points' scale
$score_points = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// The array of points a user received (from the database query)
$user_rating_points = [1, 3, 2, 7, 3, 4, 9, 2, 10, 6, 1, 7, 10, 8, 4, 8, 9, 4, 7, 10, 5];
$user_score = array_fill_keys($score_points, 0);
foreach (array_count_values($user_rating_points) as $k => $v) {
    $user_score[$k] = $v;
}
print_r($user_score);

?>

And the output is

Array
(
    [1] => 2
    [2] => 2
    [3] => 2
    [4] => 3
    [5] => 1
    [6] => 1
    [7] => 3
    [8] => 2
    [9] => 2
    [10] => 3
)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, @Marcel Preda, that's what I was looking for. Now I know what I was missing... the array_fill() call. :(
The line $user_score = array_fill(1, 10, 0); could (and maybe should) be be based on the values from $score_points. If ever the $user_score array is changed (for example to 1..12), the code won't work anymore.
The code will also not work, if the $score_points have gaps, like in some sports: [ 25, 15, 10, 7, 5, 4, 3, 2, 1 ].
Good point. 👍 I've edited the answer to use $score_points values as keys.
1
$result = array_combine(
    $score_points,
    array_map(
        fn($score_point) => count(
            array_filter($user_rating_points, fn($value) => $value === $score_point)
        ),
        $score_points
    )
);

1 Comment

Thanks @lukas.j it works and it becomes much simpler code using the array_map() with callback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.