put user inputted numbers into ascending order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

put user inputted numbers into ascending order

Post by tail »

i have a form where a user can input numbers which are separated by commas. is there some way i can separate the numbers using the explode function and then organize the numbers into ascending (least to greatest) order?
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

I'm using this but when I submit the number 1 comes up:

Code: Select all

$predata = $_POST['data'];
$data = sort(explode(", ", $predata));
echo $data;
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You can't just echo an array. Try print_r().
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Code: Select all

$predata = $_POST['data'];
$data = sort(explode(", ", $predata));
print_r ($data);
outputs the number 1
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

sort doesn't return arrays. It sorts the array you passed in, temporary array in your case which you can't use later.

Code: Select all

$data = array(2,3,1);
sort($data);
var_dump($data); // prints 1,2,3
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to do this a little bit differently than you are now. sort() doesn't return a value, it just reorganizes your array.

Code: Select all

<?php
$predata = $_POST['data'];
$data = explode(", ", $predata);
sort($data);
print_r($data);
?>
Edit | Weirdan beat me to it.
Last edited by RobertGonzalez on Thu May 31, 2007 7:07 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Weirdan wrote:sort doesn't return arrays. It sorts the array you passed in, temporary array in your case which you can't use later.

Code: Select all

$data = array(2,3,1);
sort($data);
var_dump($data); // prints 1,2,3
Everah wrote:You need to do this a little bit differently than you are now. sort() doesn't return a value, it just reorganizes your array.

Code: Select all

<?php
$predata = $_POST['data'];
$data = explode(", ", $predata);
sort($data);
print_r($data);
?>
I've never used sort before. Didn't know it returned boolean. Seems inconsistent to the way most of the PHP functions work.

Good thing there's an entire manual and function reference. ^_^
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

i tried this:

Code: Select all

$data = explode(",", $_POST['data']);
sort($data);
var_dump($data);
but this came up:

Code: Select all

array(5) { [0]=>  string(2) "12" [1]=>  string(2) "23" [2]=>  string(2) "34" [3]=>  string(2) "45" [4]=>  string(2) "63" }
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Good. Now use that data.

Oh, and a suggestion... When you do use it, typecast it to an integer (I'm not sure if you need to trim() it first if there are spaces... You should try it out).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tail wrote:i tried this:

Code: Select all

$data = explode(",", $_POST['data']);
sort($data);
var_dump($data);
but this came up:

Code: Select all

array(5) { [0]=>  string(2) "12" [1]=>  string(2) "23" [2]=>  string(2) "34" [3]=>  string(2) "45" [4]=>  string(2) "63" }
That is exactly what you want. Now you have an array that looks like this:

Code: Select all

array(5) { 
  [0]=>  string(2) "12" 
  [1]=>  string(2) "23" 
  [2]=>  string(2) "34" 
  [3]=>  string(2) "45" 
  [4]=>  string(2) "63" 
}
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Is there a way to just output the numbers?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

A foreach loop... Using the array indices... Pick one. :-p
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Thanks! This worked:

Code: Select all

$data = explode(", ", $_POST['data']);
sort($data);
foreach ($data as $num) {
	echo ''.$num.', ';
}
3 more questions:
1. is there a way to make the comma not show up on the last result?
2. is there a way to extract just the smallest integer?
3. is there a way to extract just the largest integer?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

1. is there a way to make the comma not show up on the last result?

Code: Select all

echo implode(', ', $data);
2. is there a way to extract just the smallest integer?

Code: Select all

sort($data);
echo $data[0];
3. is there a way to extract just the largest integer?

Code: Select all

rsort($data);
echo $data[0];
enjoy
Post Reply