Skip to content

Commit 6c2e858

Browse files
committed
1.2.1
1 parent d6d68e8 commit 6c2e858

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

Arr.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,42 @@
1515
class Arr
1616
{
1717
/**
18-
* Subval sort
18+
* Sorts a multi-dimensional array by a certain column
1919
*
20-
* $new_array = Arr::subvalSort($old_array, 'sort');
20+
* $new_array = Arr::sort($old_array, 'title');
2121
*
22-
* @param array $array Array
23-
* @param string $subkey Key
24-
* @param string $order Order type DESC or ASC
22+
* @param array $array The source array
23+
* @param string $field The name of the column
24+
* @param string $direction Order type DESC (descending) or ASC (ascending)
25+
* @param const $method A PHP sort method flag or 'natural' for natural sorting, which is not supported in PHP by sort flags
2526
* @return array
2627
*/
27-
public static function subvalSort(array $array, string $subkey, string $order = 'ASC') : array
28+
public static function sort(array $array, string $field, string $direction = 'ASC', $method = SORT_REGULAR) : array
2829
{
29-
if (count($array) != 0 || (!empty($array))) {
30-
foreach ($array as $k => $v) {
31-
$b[$k] = function_exists('mb_strtolower') ? mb_strtolower($v[$subkey]) : strtolower($v[$subkey]);
30+
if (count($array) > 0) {
31+
32+
// Create the helper array
33+
foreach ($array as $key => $row) {
34+
$helper[$key] = function_exists('mb_strtolower') ? mb_strtolower($row[$field]) : strtolower($row[$field]);
3235
}
33-
if ($order == null || $order == 'ASC') {
34-
asort($b);
35-
} elseif ($order == 'DESC') {
36-
arsort($b);
36+
37+
// Sort
38+
if($method === SORT_NATURAL) {
39+
natsort($helper);
40+
($direction === 'DESC') and $helper = array_reverse($helper);
41+
} elseif ($direction == 'DESC') {
42+
arsort($helper, $method);
43+
} else {
44+
asort($helper, $method);
3745
}
38-
foreach ($b as $key => $val) {
39-
$c[] = $array[$key];
46+
47+
// Rebuild the original array
48+
foreach ($helper as $key => $val) {
49+
$result[$key] = $array[$key];
4050
}
41-
return $c;
51+
52+
// Return result array
53+
return $result;
4254
}
4355
}
4456

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v1.2.1, 2018-04-26
2+
* New method sort() instead of subvalSort()
3+
14
# v1.2.0, 2018-04-26
25
* New method json() for converting an array to a JSON string
36
* New method first() to return the first element of an array

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ composer require flextype-components/arr
1616
use Flextype\Component\Arr\Arr;
1717
```
1818

19-
Subval sort
19+
Sorts a multi-dimensional array by a certain column
2020
```php
21-
$new_array = Arr::subvalSort($old_array, 'sort');
21+
$new_array = Arr::sort($old_array, 'title');
2222
```
2323

2424
Sets an array value using "dot notation".

0 commit comments

Comments
 (0)