Log in

View Full Version : sorting arrays



james438
02-29-2008, 05:42 AM
It's a little more complicated than it sounds.

$key[anderson]=12000045value;
$key[mike]=12000047reverse;
$key[tom]=21000047absent;

how do you sort by character#7 on to the end of the value in alphanumeric order? I want to ignore the first 6 characters of the value of the arrays. I've thought of removing the first 6 characters and reinserting it back into the value after it has been sorted, but just can't seem to wrap my mind around it.

james438
02-29-2008, 08:13 AM
figured it out. It was complicated too, because I had to insert the array keys into the value between the numbers and the text as text and remove it after the array was sorted and the split arrays were spliced back together. I'll try to post the code a bit later. The code is short and not too complicated, but figuring out how to do it was.

$key[anderson]=12000045value;
$key[mike]=12000047reverse;
$key[tom]=21000047absent;
foreach ($key as $name => $row1){}

$new=array();$new1=array();$new2=array();$row3=array();
$count_n=count($name);$a=0;
while ($a<=$count_n)
{$new["$name[$a]"]=$row1[$a];
$a++;}
foreach ($new as $name1 => $row2)
{
$row3=substr("$row2",0,5);
$row2=substr("$row2",5);
$row2=substr_replace($row2,$name1,3,0);
$new["$name1"]=$row2;
$new1["$name1"]=$row3;
}
asort($new);
foreach($new as $name1 => $row2)
{$row2=str_replace("$name1","",$row2);$new["$name1"]="$new1[$name1]$row2";}

In the first foreach I separated $key into two arrays: one for the keys and one for the values.

In the while statement (which I'll probably change to a foreach since I no longer need to avoid certain parts of the array) I started a new array which is a duplicate of the original array.

In the second foreach statement I split the duplicate array $new, removed the first 5 characters of the value and inserted the key name into the value between the numbers and the text, and saved the 5 removed digits for later.

Next I sorted the array, which will now sort the array by value and then by key.
In the last foreach the key names are removed from the values where they were inserted and the first 5 numbers that were removed earlier are put back where they were thus returning the values to their original value.

I hope that wasn't too complicated. I am pretty sure that I can simplify it further, but the above code was the first working version I came up with. The above code is pretty close to what I am using, but with a few minor changes.

djr33
02-29-2008, 08:29 AM
<?php
usort($array, create_function('$a,$b','return strcmp(substr($a,7), substr($b,7));'));
?>

james438
02-29-2008, 08:41 AM
see, now it's code like that that makes me wish I knew how to create functions or read them. Oh, well. You did in one line what I did in around 20 or so :p.

djr33
02-29-2008, 08:56 AM
Actually, this is more versatile:

<?php
function offset_sort($array,$n=0) {
usort($array, create_function('$a,$b','return strcmp(substr($a,'.$n.'), substr($b,'.$n.'));'));
}
offset_sort($array,7);
?>


By the way, this isn't that complex, actually. usort uses a user defined function. I could just write the function out (the first argument is the variables $a, $b; the second is the php source of the function), and then just call it using 'function_name' instead of create_function().
The function itself just uses strcmp(), which compares strings and returns a proper value for usort(), and it includes an offset for the input strings.