put user inputted numbers into ascending order
Moderator: General Moderators
put user inputted numbers into ascending order
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?
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;- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
You can't just echo an array. Try print_r().
Code: Select all
$predata = $_POST['data'];
$data = sort(explode(", ", $predata));
print_r ($data);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- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You need to do this a little bit differently than you are now. sort() doesn't return a value, it just reorganizes your array.
Edit | Weirdan beat me to it.
Code: Select all
<?php
$predata = $_POST['data'];
$data = explode(", ", $predata);
sort($data);
print_r($data);
?>
Last edited by RobertGonzalez on Thu May 31, 2007 7:07 pm, edited 1 time in total.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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
I've never used sort before. Didn't know it returned boolean. Seems inconsistent to the way most of the PHP functions work.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); ?>
Good thing there's an entire manual and function reference. ^_^
i tried this:
but this came up:
Code: Select all
$data = explode(",", $_POST['data']);
sort($data);
var_dump($data);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" }- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
That is exactly what you want. Now you have an array that looks like this:tail wrote:i tried this:but this came up:Code: Select all
$data = explode(",", $_POST['data']); sort($data); var_dump($data);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" }
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"
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Thanks! This worked:
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?
Code: Select all
$data = explode(", ", $_POST['data']);
sort($data);
foreach ($data as $num) {
echo ''.$num.', ';
}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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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];