Jump to content

Order Array By X Value


Linus Gates

Recommended Posts

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,873
Bob Smith 100 1,038,873
Mister 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,873
Mister Smith 25 420,873
Joe Blow 10 38,873[/color]

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

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

@ 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?
Link to comment
Share on other sites

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 result
echo '<pre>', print_r($persons, true), '</pre>';
?>[/code]
Link to comment
Share on other sites

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