Jump to content

sort an array but ignore a part of the values (better explained in the post)


tastro

Recommended Posts

hi,

 

also i have an array with this values:

 

$a=array('111-aa-zz-4','222-aa-zz-6','333-aa-zz-5');

 

and i want to sort(); this array, but it must ignore the numbers behind -aa-zz-

 

also 4,6,5

 

how should i do this?

 

best regards, tastro

Link to comment
Share on other sites

so you want to keep everything but the last number on the end?

 

yes i want to sort the values so that it ignores the last number on the end. but i need the number afterwards. also it mustn't erase the number at the end.

Link to comment
Share on other sites

i still need the numbers at the end of the strings/values then on the end when echoing it. i just need the sort(); function to ignore them (the numbers at the end behind -aa-zz-) when sorting. the sort(); should sort only till aa-zz- and not by the number at the end. :)

 

for example, this doesn't work the right way now:

 

<?php
$a=array(
'222-aa-zz-6',
'333-aa-zz-5',
'333-aa-zz-4'
);
sort($a);
foreach($a as $key=>$values){

$strpos=strpos($values, "z-");
$values1=substr($values,0,$strpos);
echo $values.' ('.$values1.')<br />';
}
?>

 

as it echoes this:

 

222-aa-zz-6 (222-aa-z)

333-aa-zz-4 (333-aa-z)

333-aa-zz-5 (333-aa-z)

 

instead of this:

 

222-aa-zz-6 (222-aa-z)

333-aa-zz-5 (333-aa-z)

333-aa-zz-4 (333-aa-z)

Link to comment
Share on other sites

i still need the numbers at the end of the strings/values then on the end when echoing it. i just need the sort(); function to ignore them (the numbers at the end behind -aa-zz-) when sorting. the sort(); should sort only till aa-zz- and not by the number at the end. :)

 

On second thought, why do you want to not sort on the numbers at the end?  111-aa-zz- will always come before 222-aa-zz- regardless of what number is on the end.  The only time that number will matter is if you have something like 111-aa-zz-1 and 111-aa-zz-2, in which case, does it matter to your usage?  What is the problem with just using sort?

Link to comment
Share on other sites

i still need the numbers at the end of the strings/values then on the end when echoing it. i just need the sort(); function to ignore them (the numbers at the end behind -aa-zz-) when sorting. the sort(); should sort only till aa-zz- and not by the number at the end. :)

 

for example, this doesn't work the right way now:

 

<?php
$a=array(
'222-aa-zz-6',
'333-aa-zz-5',
'333-aa-zz-4'
);
sort($a);
foreach($a as $key=>$values){

$strpos=strpos($values, "z-");
$values1=substr($values,0,$strpos);
echo $values.' ('.$values1.')<br />';
}
?>

 

as it echoes this:

 

222-aa-zz-6 (222-aa-z)

333-aa-zz-4 (333-aa-z)

333-aa-zz-5 (333-aa-z)

 

instead of this:

 

222-aa-zz-6 (222-aa-z)

333-aa-zz-5 (333-aa-z)

333-aa-zz-4 (333-aa-z)

thought thats what you wanted...and abra is right... the numbers on the end are irrelevant when using sort()

Link to comment
Share on other sites

Here's teh function using usort.

 

It's still sorting by the last number for some funny reason.

 

<?php

$a=array(
'222-aa-zz-6',
'333-aa-zz-4',
'333-aa-zz-5',
'333-aa-zz-3',
'111-whatever',
'123-somethingelse',
'312-lala',
'213-morevars'
);

usort( $a, 'ucomp' );

print_r( $a );


function ucomp( $a,$b ) {
$a = (int) substr($a,0,3);
$b = (int) substr($b,0,3);
return ( $a>$b ? 1 : ($a<$b ? -1 : 0) );
}

?>

Link to comment
Share on other sites

i still need the numbers at the end of the strings/values then on the end when echoing it. i just need the sort(); function to ignore them (the numbers at the end behind -aa-zz-) when sorting. the sort(); should sort only till aa-zz- and not by the number at the end. :)

 

On second thought, why do you want to not sort on the numbers at the end?  111-aa-zz- will always come before 222-aa-zz- regardless of what number is on the end.  The only time that number will matter is if you have something like 111-aa-zz-1 and 111-aa-zz-2, in which case, does it matter to your usage?  What is the problem with just using sort?

 

because there are not always number at the end. there are usernames, etc. and it should be ordered only by the title which is at the begining of the string.

Link to comment
Share on other sites

<?php

$a=array(
'222-aa-zz-6',
'333-aa-zz-4',
'333-aa-zz-5',
'333-aa-zz-3',
'111-whatever',
'123-somethingelse',
'312-lala',
'213-morevars'
);

sort( $a, SORT_NUMERIC );

print_r( $a );

 

it still does the same:

 

Array ( [0] => 111-whatever [1] => 123-somethingelse [2] => 213-morevars [3] => 222-aa-zz-6 [4] => 312-lala [5] => 333-aa-zz-3 [6] => 333-aa-zz-4 [7] => 333-aa-zz-5 )

 

it still doesn't ignore the last number, also it still orders by the last number. :(

Link to comment
Share on other sites

<?php

$a=array(
'222-aa-zz-6',
'333-aa-zz-4',
'333-aa-zz-5',
'333-aa-zz-3',
'111-whatever',
'123-somethingelse',
'312-lala',
'213-morevars'
);

sort( $a, SORT_NUMERIC );

print_r( $a );

 

it still does the same:

 

Array ( [0] => 111-whatever [1] => 123-somethingelse [2] => 213-morevars [3] => 222-aa-zz-6 [4] => 312-lala [5] => 333-aa-zz-3 [6] => 333-aa-zz-4 [7] => 333-aa-zz-5 )

 

it still doesn't ignore the last number, also it still orders by the last number. :(

i dont understand how this wouldnt be what you are look for...if your have a few strings that start with 333....it has to order those somehow

Link to comment
Share on other sites

<?php

$a=array(
'222-aa-zz-6',
'333-aa-zz-4',
'333-aa-zz-5',
'333-aa-zz-3',
'111-whatever',
'123-somethingelse',
'312-lala',
'213-morevars'
);

sort( $a, SORT_NUMERIC );

print_r( $a );

 

it still does the same:

 

Array ( [0] => 111-whatever [1] => 123-somethingelse [2] => 213-morevars [3] => 222-aa-zz-6 [4] => 312-lala [5] => 333-aa-zz-3 [6] => 333-aa-zz-4 [7] => 333-aa-zz-5 )

 

it still doesn't ignore the last number, also it still orders by the last number. :(

i dont understand how this wouldnt be what you are look for...if your have a few strings that start with 333....it has to order those somehow

 

yes... it should order them but not the whole string. it should ignore the last numbers... also order it only till aa-zz-

 

also

 

333-aa-zz-4

333-aa-zz-3

333-aa-zz-5

 

should stay like this:

 

333-aa-zz-4

333-aa-zz-3

333-aa-zz-5

 

and not be ordered like that:

 

333-aa-zz-3

333-aa-zz-4

333-aa-zz-5

Link to comment
Share on other sites

i found a way that works for me myself. but anyways... thanks all who tryed it. ;)

 

also i made it like this:

 

$a=array('333-aa-zz-4','333-aa-zz-6','333-aa-zz-5');

for($i=0;$<count($a);$i++){

$a[]=str_replace('-aa-zz-','-aa-zz-'.$i.'-',$a[$i]);

}

sort($a);

$a=preg_replace("@(\-aa\-zz\-)[0-9]+\-@i",'$1',$a);

 

and it worked this way. the last numbers where ignored. ;)

 

thanks to all one more time!

 

best regards, tastro

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.