/    Sign up×
Community /Pin to ProfileBookmark

array_multisort();

Hi all! The following code is fully functional. I am having trouble with array_multisort(); It is re-ordering the first array but not the second. Here is an example of the arrays the script makes to start with:

[code=html]$order
Array
(
[0] => 20
[1] => 10
)
[/code]

[code=html]$mxhosts
Array
(
[0] => smtp.lcn.biz
[1] => our-den.moralet.com
)
[/code]

and here is the script:

[code=php]<?php
function win32_getmxrr($hostname, &$mxhosts, &$Weights = NULL)
{
$mxhosts = array();
exec(‘nslookup -type=mx ‘.$hostname, $result_arr);
foreach($result_arr as $line)
{
if (preg_match(“/.*mail exchanger = (.*)/”, $line, $matches))
$mxhosts[] = $matches[1];
if (preg_match(“/.*MX preference = ([0-9]*)/”, $line, $weight)){
$order[] = $weight[1];
}
}

var_dump($order); // This is just for test purposes
echo ‘<br>’;
var_dump($mxhosts);
echo ‘<br>’; // End: this is just for test purposes

if (count($order) > 0)
{
array_multisort($order, $mxhosts); // This is what I am having trouble with. It is not sorting the $mxhosts array.
if (!is_null($Weights))
{
$Weights = $order;
}

var_dump($order); // This is just for test purposes
echo ‘<br>’;

var_dump($mxhosts);
echo ‘<br>’; // End: this is just for test purposes

return True;
}
else
{
return False;
}

} // End function

//test the function
$weight = array();
echo <<<END
<br><form method=”post” action=”http://{$_SERVER[‘HTTP_HOST’]}{$_SERVER[‘PHP_SELF’]}”>
Enter domain to test:
<input type=”text” name=”domain” id=”domain” value=”moralet.com”>
<input id=”submit” type=”submit” value=”Send”>
</form>
END;

if($_POST[‘domain’]){
$getmxrr = win32_getmxrr($_POST[‘domain’], $mxhosts, $weight);
if(!empty($getmxrr)){
$i = 1;
echo ‘Number of MX records: ‘.count($mxhosts).'<br>’;
foreach($mxhosts as $key => $value){
print ‘<br>Record ‘.$i.’: ‘.$value.'<br>Preference: ‘.$weight[$key].'<br>’;
$i++;
}
}else{
echo ‘No MX record found.’;
}
} // End: test the function
?>[/code]

This is what I end up with after array_multisort(); As you will see only the first array has been sorted. I want both sorted in the same fashion.

[code=html]$order
Array
(
[0] => 10
[1] => 20
)[/code]

[code=html]$mxhosts
Array
(
[0] => smtp.lcn.biz
[1] => our-den.moralet.com
)
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@BeachSideJun 29.2005 — Hey Bokeh this is unrelated to your question but I don't know if you realize it but this line is going to give you trouble in the future...
[code=php]
//END; Need to be on it's own line
END;if($_POST['domain']){

// Like this
END;
if($_POST['domain']){
[/code]
Copy linkTweet thisAlerts:
@bokehauthorJun 29.2005 — Hey Bokeh this is unrelated to your question but I don't know if you realize it but this line is going to give you trouble in the future...
[code=php]
//END; Need to be on it's own line
END;if($_POST['domain']){

// Like this
END;
if($_POST['domain']){
[/code]
[/QUOTE]

It's not like that in the original. The forum is responsible for that. Thanks anyway.
Copy linkTweet thisAlerts:
@BeachSideJun 29.2005 — Ok cool...

As to your array_multisort problem have you tried comparing them by strings with SORT_STRING?
Copy linkTweet thisAlerts:
@bokehauthorJun 29.2005 — I've written it a bit different now and it seams to work but I really hate not to understand why if something does not work. Could it be that the first array is storing the numbers as strings instead of integers?
×

Success!

Help @bokeh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...