tastro Posted June 1, 2011 Share Posted June 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/ Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 so you want to keep everything but the last number on the end? Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223468 Share on other sites More sharing options...
AbraCadaver Posted June 1, 2011 Share Posted June 1, 2011 Write a function that compares the values using strpos() or preg_match() and then pass the array and function name to usort(). Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223472 Share on other sites More sharing options...
tastro Posted June 1, 2011 Author Share Posted June 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223474 Share on other sites More sharing options...
tastro Posted June 1, 2011 Author Share Posted June 1, 2011 Write a function that compares the values using strpos() or preg_match() and then pass the array and function name to usort(). AbraCadaver could you give me an example in php code? thank you Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223477 Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 sort($a); foreach($a as $key=>$values) { $strpos = strpos($values, "z-"); $values = substr($values,0,$strpos); echo $values; } Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223483 Share on other sites More sharing options...
tastro Posted June 1, 2011 Author Share Posted June 1, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223618 Share on other sites More sharing options...
AbraCadaver Posted June 1, 2011 Share Posted June 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223621 Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 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() Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223624 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 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) ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223660 Share on other sites More sharing options...
tastro Posted June 1, 2011 Author Share Posted June 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223696 Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 <?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 ); Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223704 Share on other sites More sharing options...
tastro Posted June 2, 2011 Author Share Posted June 2, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223988 Share on other sites More sharing options...
tastro Posted June 2, 2011 Author Share Posted June 2, 2011 maybe that it would be possible to somehow split 333-aa-zz-5 into 333-aa-zz- and 5 and then afterwards put them together again somehow so that when sorting, it would ignore the last number. Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1223991 Share on other sites More sharing options...
fugix Posted June 2, 2011 Share Posted June 2, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1224002 Share on other sites More sharing options...
tastro Posted June 2, 2011 Author Share Posted June 2, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1224365 Share on other sites More sharing options...
tastro Posted June 3, 2011 Author Share Posted June 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238093-sort-an-array-but-ignore-a-part-of-the-values-better-explained-in-the-post/#findComment-1224589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.