<?php
$c = count($rank); // 5
for ($i = 0; $i < $c; $i++) {
$labels [] = array("value" =>$i, "text" => $i);
}
?>
output: `[{"value":1,"text":1},{"value":2,"text":2},{"value":3,"text":3},{"value":4,"text":4},{"value":5,"text":5}]`
But what I need is:
[{"value":5,"text":1},{"value":4,"text":2},{"value":3,"text":3},{"value":2,"text":4},{"value":1,"text":5}]
Any idea about that?
I’ll describe my line of thinking.
What is the pattern in the sequence
5, 4, 3, 2, 1? Quite clearly, I decrease by one each time. I already know that$iincreases by one each time, because that is how we wrote our for loop. My goal and what is available with$iis fairly close, so is there any way I can use$i?Indeed there is. Instead of saying the sequence
5, 4, 3, 2, 1decreases by one each time, I can say that the sequence increases in its distance from 5 by one each time. That is, the sequence is equivalent to5 - 0, 5 - 1, 5 - 2, 5 - 3, 5 - 4. Notice that this lines up perfectly with$i. Therefore, our solution can be the following:This takes a bit of intuition to see, and if you are in a similar situation and cannot figure out the pattern, you can always introduce a new variable.