0

I get two arrays from

$check = getmxrr ( $domain, $mxhosts, $weight );

I want to get the mxhosts in a third array from lowest to highest weight.

For simplicity let's have simple array names and values:

$s = array('slowest','fast','very slow', 'slow','very fast', 'fastest');
$n = array( 40,       10,    30,          20,    5,           0       );

Now, with this code, I can get the ordered array I'm looking for:

$o = array_combine($n, $s);
ksort($o);

But, if there are elements with the same weight, only the last element with the repeated weight would appear in the ordered array.

Is there any similar (low footprint) solution without this issue?

Illustrative cases follow.

Weight values are unique. Output is OK:

[0] => fastest
[5] => very fast
[10] => fast
[20] => slow
[30] => very slow
[40] => slowest

We have same-weight elements. Output is NOT OK (fast is gone):

$n = array( 40,       10,    30,         10,       20,    5,           0       );
$s = array('slowest','fast','very slow','fast2', 'slow','very fast', 'fastest');
[0] => fastest
[5] => very fast
[10] => fast2
[20] => slow
[30] => very slow
[40] => slowest
8
  • share code for array_combine method Commented May 31, 2012 at 11:43
  • it makes a new array $o with keys from $s and value from $n by order $o[$n[0]] = $s[0] Commented May 31, 2012 at 11:45
  • @CoDeaDDict it's a native function: php.net/manual/en/function.array-combine.php Commented May 31, 2012 at 11:45
  • I'm trying to understand if two elements has the same weight ksort won't sort the array by the keys correctly? Can you show how it sorts it? Commented May 31, 2012 at 11:46
  • and how would you like the array as well ;-), maybe natsort() would do the trick ... depends on what you want exactly ... Commented May 31, 2012 at 11:47

3 Answers 3

2

Since you believe the host names are unique, switch the order of the combine

$n = array( 40,       10,    30,         10,       20,    5,           0       );
$s = array('slowest','fast','very slow','fast2', 'slow','very fast', 'fastest');
$o = array_combine($s, $n);

and use asort to sort on values while maintaining keys

asort($o);

than your sorted host names are in array_keys

var_dump(array_keys($o));
array(7) {
  [0]=>
  string(7) "fastest"
  [1]=>
  string(9) "very fast"
  [2]=>
  string(5) "fast2"
  [3]=>
  string(4) "fast"
  [4]=>
  string(4) "slow"
  [5]=>
  string(9) "very slow"
  [6]=>
  string(7) "slowest"
}

var_dump($o);
array(7) {
  ["fastest"]=>
  int(0)
  ["very fast"]=>
  int(5)
  ["fast2"]=>
  int(10)
  ["fast"]=>
  int(10)
  ["slow"]=>
  int(20)
  ["very slow"]=>
  int(30)
  ["slowest"]=>
  int(40)
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this:

$s = array('slowest','fast', 'slow', 'fastest', 'slow 2');
$n = array( 40, 10, 20, 0, 20);

$res = array();
foreach ($n as $k => $v) {
    $res[$v] .= count($res[$v]) ? ",{$s[$k]}" : $s[$k];
}

ksort($res);
$res = explode(',', implode(',', $res));

print_r($res);
/*
Array
(
    [0] => fastest
    [1] => fast
    [2] => slow
    [3] => slow 2
    [4] => slowest
)
*/

2 Comments

The value of wights are not important; only they need to be in the correct order. So, all I want in the above case is ('fastest', 'fast', 'slow', 'slow 2', 'slowest')
Thank you for the effort. This code does get the job done, but generates many notices.
0

Try this

function cmp($a, $b)
{
    if ($a > $b)
        return 1;
    elseif ($b > $a)
        return -1;
    else
       return 0;
}

uksort($array, 'cmp');

1 Comment

Is $array in the above, the $weight array? If so, what effect would that have on $mxhosts array? -- $mxhosts is what I need sorted as per values in $weight though

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.