Jump to content

Array sorting issue


ReJoshua

Recommended Posts

Generally I am able to help my self on the forums. But, this one has me stumped. I've done it before and I don't remember it being this hard. The array looks something like so

 

$users['id'] = '00001','00002','0003'

$users['jobs'] = '22','322','112'

$users['connected'] = '12','210','89'

$users['percent'] = '54','65','89'

 

I've used so much different things to try and take those, keep the data together (index 0 might be moved to 2, but all values from 0 are moved to 2) and sort by 'percent'. So I can display the person that has the highest percentage first. Basically the data was originally a CSV that is now in a database and is pulled, percentages calculated and then displayed. But the order is off. There is a bunch more data then show but I'm using PHP to do all the math for adding up jobs, seeing whats connected right and then coming up with a percentage, sticking it all in an array so I can sort it; but for some reason I cannot remember how I've done this before.

Link to comment
Share on other sites

It kind of worked. Put everything out of order. IE just reordered the 'percent' column and left the others where they were (which is by default 'id'). But I figured out how to do it using that and some other code. Here is the below code I found and made it work perfectly. Thanks for the direction, helped a bunch.

 

$sort_direction = 'SORT_DESC';
$sort_field = 'percent';
$sort_arr = array();
foreach($users AS $uniqid => $row){
foreach($row AS $key=>$value){
	$sort_arr[$key][$uniqid] = $value;
}
}
array_multisort($sort_arr[$sort_field], constant($sort_direction), $users);

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.