Linus Gates Posted August 24, 2006 Share Posted August 24, 2006 Below is an oversimplified version of my code. The reason being, I only really need to convey the idea of what I need to do....[code]<?php$info = '(THE STRING THAT CONTAINS ALL THE DATA I NEED TO SORT)';$info = explode("\n",$info);foreach($info as $results){preg_match('/SOME REGEX CRITERIA HERE/', $results, $name);preg_match('/SOME REGEX CRITERIA HERE/', $results, $deducttions);preg_match('/SOME REGEX CRITERIA HERE/', $results, $income);$person[name] = $name[0];$person[name] = $deducttions[0];$person[income] = $income[0];echo"$person[name] $person[deducttions] $person[income]<br />";}?>[/code]The above code basically takes a string of text that is NOT available in a database, filters out only the data I need, and then assigns the values I need to their corresponding variables. Up to that point, the [color=blue][b]$person[/b][/color] array looks like this:[code]Array ( [name] => Joe Blow [deducttions] => 10 [income] => 38,873 )Array ( [name] => Bob Smit [deducttions] => 100 [income] => 1,038,873 )Array ( [name] => Mister Smith [deducttions] => 25 [income] => 420,873 )[/code]In turn, it echos out as such:[color=blue]Joe Blow 10 38,873Bob Smith 100 1,038,873Mister Smith 25 420,873[/color][u]Question[/u]:What I actually need done, is to echo out the rows/results of the [color=blue][b]$person[/b][/color] array, in order from greatest to least, using the value of [color=blue][b]$person[/b][income][/color]. So when printed or echoed out, it would look like below, regardless of the original order. :[color=blue]Bob Smith 100 1,038,873Mister Smith 25 420,873Joe Blow 10 38,873[/color]Thanks in advance and I hope I was clear enough. :) Quote Link to comment https://forums.phpfreaks.com/topic/18561-order-array-by-x-value/ Share on other sites More sharing options...
wildteen88 Posted August 24, 2006 Share Posted August 24, 2006 You'll probably want to use [url=http://php.net/manual/en/function.asort.php]asort()[/url][code=php:0]<?php$person = array( 'name' => array('Bob', 'Sam', 'Pete'), 'income' => array('58,523', '23,585', '156,899') );echo 'Before sort:<br /><br /><pre>' . print_r($person, true) . '</pre>';arsort($person['income'], SORT_NUMERIC);echo 'After sort:<pre>' . print_r($person, true) . '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18561-order-array-by-x-value/#findComment-79940 Share on other sites More sharing options...
Caesar Posted August 24, 2006 Share Posted August 24, 2006 @ wild:Hmmmm...I was doing something similar but ran into the same problems when dealing with loops. The example you're showing asumes you know the values for the $person[income]. What if you're pulling the info from a file that completely changes all the time? Or how about $_POSTED data, as I was dealing with (In my project)? I think (From looking at the first post) that would be the case since regular expressions are being used to retrieve the values.Now, how would you implement that within the foreach() loop for instance? Quote Link to comment https://forums.phpfreaks.com/topic/18561-order-array-by-x-value/#findComment-80044 Share on other sites More sharing options...
Barand Posted August 24, 2006 Share Posted August 24, 2006 Linus,use a custom sort function with usort()[code]<?php$persons = array ( array ( 'name' => 'Joe Blow', 'deducttions' => 10, 'income' => 38873 ), array ( 'name' => 'Bob Smit', 'deducttions' => 100, 'income' => 1038873 ), array ( 'name' => 'Mister Smith', 'deducttions' => 25, 'income' => 420873 ));function income_sort($a, $b) { if ($a['income'] == $b['income']) return 0; return $a['income'] < $b['income'] ? 1 : -1;}usort ($persons, 'income_sort');// view resultecho '<pre>', print_r($persons, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18561-order-array-by-x-value/#findComment-80072 Share on other sites More sharing options...
Caesar Posted August 24, 2006 Share Posted August 24, 2006 Barand, I was actually working on the same thing recently but put it off. And as Linus did, I was using a loop. I don't know if his source string is from a file but, I needed to first clean up the data using regex. The source of the data was via a $_POST. And since the contents of the data was unknown to me, I needed to use regex to match only the criteria I needed to work with. (lose you yet?) What I'm asking, is how you would implement that logic, within a loop. When $person's value is unknown. (I've seen all the examples. And all examples asume you know the values beforehand.)Edit: Linus, what is the source of your string? Quote Link to comment https://forums.phpfreaks.com/topic/18561-order-array-by-x-value/#findComment-80087 Share on other sites More sharing options...
Barand Posted August 24, 2006 Share Posted August 24, 2006 Not too sure what you asking, but you can loop through the POSTed data with a foreach loopforeach ($_POST as $formfieldname => $value) { // process fields} Quote Link to comment https://forums.phpfreaks.com/topic/18561-order-array-by-x-value/#findComment-80092 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.