Page 1 of 3

put user inputted numbers into ascending order

Posted: Thu May 31, 2007 5:56 pm
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?

Posted: Thu May 31, 2007 5:59 pm
by pedrotuga

Posted: Thu May 31, 2007 6:23 pm
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;

Posted: Thu May 31, 2007 6:54 pm
by superdezign
You can't just echo an array. Try print_r().

Posted: Thu May 31, 2007 7:02 pm
by tail

Code: Select all

$predata = $_POST['data'];
$data = sort(explode(", ", $predata));
print_r ($data);
outputs the number 1

Posted: Thu May 31, 2007 7:05 pm
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

Posted: Thu May 31, 2007 7:07 pm
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.

Posted: Thu May 31, 2007 7:07 pm
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. ^_^

Posted: Thu May 31, 2007 7:24 pm
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" }

Posted: Thu May 31, 2007 7:28 pm
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).

Posted: Thu May 31, 2007 7:38 pm
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" 
}

Posted: Thu May 31, 2007 7:47 pm
by tail
Is there a way to just output the numbers?

Posted: Thu May 31, 2007 7:50 pm
by superdezign
A foreach loop... Using the array indices... Pick one. :-p

Posted: Thu May 31, 2007 8:09 pm
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?

Posted: Thu May 31, 2007 8:12 pm
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