-1

I have a array like this:

$row_arr_1=array(7,9,5,10);

now I want to get the result array like this:

$row_arr_2=array(3,2,4,1);

Explanation:

As 10 is the largest value in row_arr_1, then it will be replaced with value 1.

Similarly, as 9 is the 2nd highest value of row_arr_1, then it will be replaced by 2 and so on.

I tried to sort the values of row_arr_1 but the position is changed.

How can i get my desired result?

Xyz
  • 5,955
  • 5
  • 40
  • 58

4 Answers4

3

It can be done using rsort() and array_search()

$row_arr_1=array(7,9,5,10);
$row_copy = $row_arr_1;
$row_arr_2 = array();
rsort($row_copy);
foreach($row_arr_1 as $val) {
    $row_arr_2[] = array_search($val, $row_copy) + 1;
}
print_r($row_arr_2);

https://eval.in/990078

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Ahsan Ali
  • 4,951
  • 2
  • 17
  • 27
1

You can use arsort() to sort the array while preserving keys, and use those keys for your array via array_keys():

$row_arr_1 = array(7,9,5,10);
$row_arr_1_backup = $row_arr_1;


arsort($row_arr_1_backup);
$row_arr_2 = array_keys($row_arr_1_backup);

asort($row_arr_2);
$row_arr_2 = array_keys($row_arr_2);


array_walk($row_arr_2, function(&$item, &$key) {
    $item = $item + 1;
});

You have to duplicate the original array, since arsort will sort the actual array it points to, rather than returning a new array.

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • Is it wrong because it is off by one? eg `Array ( [0] => 3 [1] => 1 [2] => 0 [3] => 2 )` instead of `Array ( [0] => 4 [1] => 2 [2] => 1 [3] => 3 )`? I noticed that, and it can be fixed with `array_walk`. But it is still the correct array in the sense of the new array is a ranking of the source array. – Anthony Apr 18 '18 at 06:31
  • You have to sort the keys again to get the desired output (still off by one, but still can be fixed with array_walk if needed). Answer updated. – Anthony Apr 18 '18 at 06:39
0
$row_arr_1_old = array(7, 9, 5, 10);
$row_arr_1 = array(7, 9, 5, 10);
rsort($row_arr_1);

$test = [];
foreach ($row_arr_1_old as $key => $value) {
    $test[] = array_search($value, $row_arr_1);
}

print_r($test);
Sachin Aghera
  • 486
  • 3
  • 8
  • Never post code-only answers on StackOverflow. Unexplained answers are low value on StackOverflow because they do very little to educate the OP and thousands of future researchers. If you are going to dedicate your time to supporting this community, always post complete answers. – mickmackusa Apr 20 '18 at 11:58
0

For best efficiency try to reduce total function calls; this especially means minimizing / eliminating iterated function calls.

This is my slightly more efficient version of ahsan's answer.

Code: (Demo)

$copy = $arr = [7, 9, 5, 10];
rsort($arr);                      // generates: [10, 9, 7, 5]
$flipped = array_flip($arr);      // generates: [10 => 0, 9 => 1, 7 => 2, 5 => 3]
foreach($copy as $v) {
    $result[] = ++$flipped[$v];   // adds one to each accessed value from $flipped
}
var_export($result);

Output:

array (
  0 => 3,
  1 => 2,
  2 => 4,
  3 => 1,
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136