Hi, I have an array (PHP) with 2 values: E.g. 1, apple 30, banana 23, melon I want to sort the array by the integer before the comma and string... any ideas? Regards,
yes (sorry I mean split the data) - it is an array that contains data in the following format: integer, name E.g. 25, banana So, what I need to do it sort this array by the value of the integer before the comma and name. So let's say the array contained the following: 25, banana 160, apple, 4, melon 2009, grapes I need it to be sorted into the following: 2009, grapes 160, apple, 25, banana 4, melon (DESC order)
$x=0;$newArray = array(); foreach($firstArray as $item) { list($newArray[$x][0],$newArray[$x][1]) = explode(',',$item); $x++; } PHP: newArray is now sortable. Peace,
Hi azizny, Apologies for the delay... Would you mind showing me how to integrate into my existing script? Ideally, I want to arrange $total by the 1st value. while( $ratings2 = mysql_fetch_assoc( $ratings ) ) { $points = $ratings2['points']; $name = $ratings2['name']; $total.= "Points: {$p}, Name: {$n}"; } PHP: Many Thanks!
while( $ratings2 = mysql_fetch_assoc( $ratings ) ) { $myArray[$ratings2[points]] = $ratings2['name']; } sort($myArray); foreach($myArray as $point => $name){ $total.= "Points: {$point}, Name: {$name}";} PHP: not test. Peace,
With a LIMIT of 50, it only seems to show the first 13 rows and seems to order by array integer. Eg. Points: 0, Name: Namea Points: 1, Name: Nameb Points: 2, Name: Namec Points: 3, Name: Named If you're interested, PM for IM (MSN/AIM/Skype etc...) - I'll throw some $$ in for the help.
Maybe some points are the same, try this: $myArray = array(); while( $ratings2 = mysql_fetch_assoc( $ratings ) ) { array_push($myArray,array($ratings2['points'],$ratings2['name'])); } sort($myArray); foreach($myArray as $item){ $total.= "Points: {$item[0]}, Name: {$item[1]}";} PHP: Tested. Peace,
Try different sort methods like arsort. Check PHP documentation by clicking on the sort link in the code. Peace,