Jump to content


Photo

Order Array By X Value


  • Please log in to reply
5 replies to this topic

#1 Linus Gates

Linus Gates
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 24 August 2006 - 05:25 PM

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....

<?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 />";

}
?>

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 $person array looks like this:

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  )

In turn, it echos out as such:

Joe Blow 10 38,873
Bob Smith 100 1,038,873
Mister Smith 25 420,873


Question:
What I actually need done, is to echo out the rows/results of the $person array, in order from greatest to least, using the value of $person[income]. So when printed or echoed out, it would look like below, regardless of the original order. :

Bob Smith 100 1,038,873
Mister Smith 25 420,873
Joe Blow 10 38,873


Thanks in advance and I hope I was clear enough.  :)



#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 August 2006 - 05:32 PM

You'll probably want to use asort()

<?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>';

?>


#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 24 August 2006 - 08:58 PM

@ 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?
PHP Ninja

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 August 2006 - 10:05 PM

Linus,

use a custom sort function with usort()

<?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 result
echo '<pre>', print_r($persons, true), '</pre>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 24 August 2006 - 10:47 PM

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?
PHP Ninja

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 August 2006 - 10:59 PM

Not too sure what you asking, but you can loop through the POSTed data with a foreach loop

foreach ($_POST as $formfieldname => $value) {
    // process fields
}
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users