-2

I have array of urls stored. Every url has some format. I need to sort the urls array. Like

https://s3.amazonaws.com/photos/1358086239.jpg?response-content-type=image%2Fjpeg&AWSAccessKeyId=AKIAI2U3TRTNGXNCQHYQ&Expires=1359636889&Signature=l9GnekYT0wK9AznkPWWjBuCRgBY%3D
https://s3.amazonaws.com/photos/1358066630.jpg?response-content-type=image%2Fjpeg&AWSAccessKeyId=AKIAI2U3TRTNGXNCQHYQ&Expires=1359636889&Signature=l9GnekYT0wK9AznkPWWjBuCRgBY%3D

These urls are reutned by amazon S3 PHP SDK get_object_url method.

I have 1000 of these urls but i want to sort these urls based on 1358086239.jpg value this value is everytime an integer value and i want to sort urls in Asc order based on this value.

How can i sort these urls array. i tried using php builtin function ksort() But it did not helped.

Asghar
  • 2,336
  • 8
  • 46
  • 79
  • 1
    Strip the leading URL (always same?) then sort on the resulting value. – Floris Jan 31 '13 at 12:41
  • Hint: the answer to Jan Hancic's (pardon, but I don't have the accents on my keyboard =) comment is *not*: how do I do that? The proper answer is: I tried this, it failed in this way, what am I doing wrong? – J. Steen Jan 31 '13 at 12:43
  • 1
    If everything before the `[0-9]+.jpg` is always the same, you would expect to be able to sort on the whole thing and get the right result . Why does that not work? – Floris Jan 31 '13 at 13:57

5 Answers5

2

Well, something like this...

usort($array, function($a, $b) {
  $aKey = substr($a, 33, 10);
  $bKey = substr($b, 33, 10);
  return $aKey < $bKey;
}
//$array is sorted now
jasir
  • 1,461
  • 11
  • 28
1

Try to loop through the URLs and assign each one of them into the array, where key would be your number in a file name. Then simply sort it by keys and... you're done!

What you need is:

Community
  • 1
  • 1
MarcinWolny
  • 1,600
  • 2
  • 27
  • 40
1

sed 's/photos\/\([0-9]+.jpg\)/\1/' | sort would give the file names in order. A grep -f with that as first input and URLs as second would produce your desired output.

Floris
  • 45,857
  • 6
  • 70
  • 122
1

Try this (fixed):

$urls = array(
'https://s3.amazonaws.com/photos/1358086239.jpg?response-content-type=image%2Fjpeg&AWSAccessKeyId=AKIAI2U3TRTNGXNCQHYQ&Expires=1359636889&Signature=l9GnekYT0wK9AznkPWWjBuCRgBY%3D',
'https://s3.amazonaws.com/photos/1358066630.jpg?response-content-type=image%2Fjpeg&AWSAccessKeyId=AKIAI2U3TRTNGXNCQHYQ&Expires=1359636889&Signature=l9GnekYT0wK9AznkPWWjBuCRgBY%3D',
);


$list = array();
foreach ($urls as $v) {
   $tmp = explode('https://s3.amazonaws.com/photos/', $v);
   $tmp = explode('.jpg?response-content', $tmp[1]);

   $list[$tmp[0]] = $v;
} 

ksort($list);

var_dump($list);
dotoree
  • 2,983
  • 1
  • 24
  • 29
1
<?php
$url_array  = array('https://s3.amazonaws.com/photos/1358086239.jpg?response-content-type=image%2Fjpeg&AWSAccessKeyId=AKIAI2U3TRTNGXNCQHYQ&Expires=1359636889&Signature=l9GnekYT0wK9AznkPWWjBuCRgBY%3D', 'https://s3.amazonaws.com/photos/1358066630.jpg?response-content-type=image%2Fjpeg&AWSAccessKeyId=AKIAI2U3TRTNGXNCQHYQ&Expires=1359636889&Signature=l9GnekYT0wK9AznkPWWjBuCRgBY%3D');
$res_aray = array();
foreach($url_array as $val){
   preg_match('/photos\/(?P<numb>\d+)\.jpg/', $val, $matches);   
   $res_aray[$matches['numb']] = $val;
}
ksort($res_aray);
print_r($res_aray);

?>
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73