Click to See Complete Forum and Search --> : sorting IP addresses in a multidimensional array
Angry Black Man
03-02-2007, 08:15 PM
as the title says, i need to sort the "ip address" values of my multidimensional array.
initially, i implemented a "user sort" that in turn did a string compare (strcmp). however, this made it so that 189 came before 19, and i dont want that.
i then tried to do a natural sort (natsort), but it doesn't appear that you can natsort a multidimensional array since it will complain (and rightly so) about attempting an "Array to string conversion", nothing changes, and it doesnt produce expected results. conversely, if you try to specify a specific key's value within the multi-array and then natsort by that value, it will complain (apparently rightly so) that "The argument should be an array", no changes are made, and of course you still dont get expected output.
i even tried to use "ip2long" (not knowing anything really about IPv4) hoping i'd get integers that i could compare numerically from high to low, but it didnt work.
so can anyone help me? anyone got any bright ideas? maybe on how to pad my IP's with preceding/trailing zeros and do a compare that way, perhaps?
NogDog
03-02-2007, 10:09 PM
Can you give us a sample of what the array looks like and how you want it sorted?
theuedimaster
03-03-2007, 02:30 AM
Use a foreach loop to get the multi-dimensional array down to a one-dimensional array (add each ip to one big sum array), and then sort the big sum array.
Angry Black Man
03-03-2007, 06:30 AM
can you clarify something about the one dimensional array? i dont see how once ive broken it down to a 1D array to sort it can then be used to properly sort my multi-d array afterwords? if you can, explain what i'd do after my 1D array is natsort'ed... how can i use that to get my MD array sorted?
@nog:
this is not actually my array; it has been done for the illustrative purposes of this thread. i want to numerically sort by IPs, lowest to high:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => 192.168.1.189
)
[Quagmire] => Array
(
[0] => Glenn
[1] => 192.168.1.111
)
[Brown] => Array
(
[0] => Cleveland
[1] => 192.168.1.19
)
)
so therefore, once my array is resorted, it should be sorted so that brown is first, quagmire remains second, and griffin is 3rd.
therefore, if looking at the last octet: 19 < 111 < 189
instead, i get 111 < 189 < 19 (becuase it [apparently] assumes the 19 has a trailing zero due to the strcmp i am wrongfully attempting)
theuedimaster
03-04-2007, 12:47 AM
Okay, first of all, I think array members are initially sorted by when they are added into the array. Off of that presumption, I'd do this:
Cycle through the array and make a one dimensional array with the ip addresses. Now, when you add in that ip address value, I want you to strip the periods off so you just have an integer. Now do a regular integer sort.
Since this one dimensional array is sorted, the indexes are in the right order, so use array_keys() [http://us2.php.net/manual/en/function.array-keys.php] to create an array of the index names. Now, after that, I want you to cycle through this index array. Use the index value to call the name and ip value from the original multi array. The format should be like this in the loop:
$array2[index value][0] = original multi value (name) called by index.
$array2[index value][1] = original multi value (ip) called by index.
I don't know why I didn't write the code, I was sort of lazy. Hope you understand. If you don't, I'll write it out.
NogDog
03-04-2007, 02:33 AM
<?php
// test data
$data = array
(
'Griffin' => Array
(
0 => 'Peter',
1 => '192.168.1.189'
),
'Quagmire' => Array
(
0 => 'Glenn',
1 => '192.168.1.111'
),
'Brown' => Array
(
0 => 'Cleveland',
1 => '192.168.1.19'
)
);
// sort function
function cmp($a, $b)
{
// need to use sprintf with %u to use unsigned int value
$aip = sprintf('%u', ip2long($a[1]));
$bip = sprintf('%u', ip2long($b[1]));
return($aip < $bip);
}
// sort it
uksort($data, 'cmp');
// show result
printf("<pre>%s</pre>\n", print_r($data, TRUE));
?>
Angry Black Man
03-04-2007, 07:18 AM
perfection, nog, perfection. that worked absolutely perfectly!
it scares me to think how much you are/can be making doing this for a living!
/bow