0

I have an array like this

//$src
Array(
[dim_55] => path/to/image.jpg
[dim_180] => path/to/image.jpg
[dim_190] => path/to/image.jpg
[dim_475] => path/to/image.jpg
[dim_635] => path/to/image.jpg
[dim_540] => path/to/image.jpg
[dim_1130] => path/to/image.jpg
[dim_1900] => path/to/image.jpg
)

( path/to/image.jpg is a generic path )

I would like to order this by key in an order like

dim_1900
dim_1130
dim_635
dim_540
dim_475
..

To achieve this solution I did something like:

$sortSrc = array();
foreach($src as $key => $value){
  $newkey = explode("dim_", $key);
  $sortSrc[$newkey[1]] = $value; 
}

krsort($sortSrc);

It works but maybe is not so effective. Is there a way in which I can achieve this with native PHP functions?

5
  • 4
    krsort is a native php function, or i'm missing something in your question? Commented May 28, 2015 at 10:24
  • 2
    Use the existing sort functions with the SORT_NATURAL flag: krsort($myArray, SORT_NATURAL); Commented May 28, 2015 at 10:25
  • with out loop your code works true I think Commented May 28, 2015 at 10:27
  • @OfirBaruch I was talking about the foreach Commented May 28, 2015 at 10:29
  • Can you please share the structure of $src? Commented May 28, 2015 at 10:30

4 Answers 4

4

You need to use krsort($src, SORT_NATURAL);

See: http://php.net/manual/en/function.sort.php for sort flags documentation

Sign up to request clarification or add additional context in comments.

1 Comment

It was too early eheh :)
0

you should be able to achieve what you want with ksort:

krsort — Sort an array by key in reverse order

 <?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

The above example will output:

d = lemon
c = apple
b = banana
a = orange

Comments

0
#!/usr/bin/env php
<?php
$a = [
    'dim_55' => 'path/to/image.jpg',
    'dim_180' => 'path/to/image.jpg',
    'dim_190' => 'path/to/image.jpg',
    'dim_475' => 'path/to/image.jpg',
    'dim_635' => 'path/to/image.jpg',
    'dim_540' => 'path/to/image.jpg',
    'dim_1130' => 'path/to/image.jpg',
    'dim_1900' => 'path/to/image.jpg',
];

krsort($a, SORT_NATURAL);

print_r($a);

Comments

0

you can also use custom sort by key numeric with uksort() function, try this code

<?php 
$data = Array(
"dim_55" => "path/to/image.jpg",
"dim_180" => "path/to/image.jpg",
"dim_190" => "path/to/image.jpg",
"dim_475" => "path/to/image.jpg",
"dim_635" => "path/to/image.jpg",
"dim_540" => "path/to/image.jpg",
"dim_1130" => "path/to/image.jpg",
"dim_1900" => "path/to/image.jpg"
);
$sort_data = uksort($data, function($a, $b){
  preg_match("/dim_(\d+)/",$a, $matcha);
  preg_match("/dim_(\d+)/",$b, $matchb);
  if($matcha[1] == $matchb[1]){
    return 0;
  }
  return $matcha[1] > $matchb[1] ? -1 : 1;
});
print_r($data);
?>

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.