Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

order a list numerically

Posted on 2013-06-13
4
Medium Priority
?
206 Views
Last Modified: 2013-06-19
0
Comment
Question by:rgb192
4 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39244267
You can try this:
<?php
 error_reporting('E_ALL');
                                  
$arr = array(
"http://website.com/folder/page.php?u=12853",
"http://website.com/folder/page.php?u=21684",
"http://website.com/folder/page.php?u=1550",
"http://website.com/folder/page.php?u=14754",
"http://website.com/folder/page.php?u=9912",
"http://website.com/folder/page.php?u=14577",
"http://website.com/folder/page.php?u=21252",
"http://website.com/folder/page.php?u=15503",
"http://website.com/folder/page.php?u=10548",
"http://website.com/folder/page.php?u=15190",
);            
echo "<pre>";
var_dump($arr);
echo "</pre>";
foreach($arr as $a){
  $data= explode("=", $a);
  $numbers[] = $data[1];
}
sort($numbers);
foreach($numbers as $n){
  $sortedArray[] = "http://website.com/folder/page.php?u=" . $n;
}

echo "<pre>";
var_dump($sortedArray);
echo "</pre>";

Open in new window


Cheers
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39244290
PHP has a built-in function for this.
http://php.net/manual/en/function.natsort.php

And if you're looking for alphabetic sorting, this one is often the best answer.
http://php.net/manual/en/function.natcasesort.php

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";

$arr = array
( "http://website.com/folder/page.php?u=12853"
, "http://website.com/folder/page.php?u=21684"
, "http://website.com/folder/page.php?u=1550"
, "http://website.com/folder/page.php?u=14754"
, "http://website.com/folder/page.php?u=9912"
, "http://website.com/folder/page.php?u=14577"
, "http://website.com/folder/page.php?u=21252"
, "http://website.com/folder/page.php?u=15503"
, "http://website.com/folder/page.php?u=10548"
, "http://website.com/folder/page.php?u=15190"
)
;
var_dump($arr);

natsort($arr);
print_r($arr);

Open in new window

Best regards, ~Ray
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 39257366
echo '<pre>';

$u  = array("12853","21684","1550","14754","9912","14577","21252","15503","10548","15190");

sort($u);

foreach($u as $key=>$val)
{
   $arr[] = "http://website.com/folder/page.php?u=".$val;
}

print_r($arr);
0
 

Author Closing Comment

by:rgb192
ID: 39261188
This code sorts the urls with easy function

first code sorts parsed numbers

last code sorts only numbers and requires copy paste
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question