Sort multidimension array in php by more than two fields
- by Ankita Agrawal
I want to sort array by two fields. I mean to say I have an array like :-
Array
(
[0] => Array
(
[name] => abc
[url] => http://127.0.0.1/abc/img1.png
[count] => 69
[img] => accessoire-sets_1.jpg
)
[1] => Array
(
[name] => abc2
[url] => http://127.0.0.1/abc/img12.png
[count] => 73
[img] =>
)
[2] => Array
(
[name] => abc45
[url] => http://127.0.0.1/abc/img122.png
[count] => 15
[img] => tomahawk-kopen_1.png
)
[3] => Array
(
[name] => zyz
[url] => http://127.0.0.1/abc/img22.png
[count] => 168
[img] =>
)
[4] => Array
(
[name] => lmn
[url] => http://127.0.0.1/abc/img1222.png
[count] => 10
[img] =>
)
[5] => Array
(
[name] => qqq
[url] => http://127.0.0.1/abc/img1222.png
[count] => 70
[img] =>
)
[6] => Array
(
[name] => dsa
[url] => http://127.0.0.1/abc/img1112.png
[count] => 43
[img] =>
)
[7] => Array
(
[name] => wer
[url] => http://127.0.0.1/abc/img172.png
[count] => 228
[img] =>
)
[8] => Array
(
[name] => hhh
[url] => http://127.0.0.1/abc/img126.png
[count] => 36
[img] =>
)
[9] => Array
(
[name] => rrrt
[url] => http://127.0.0.1/abc/img12.png
[count] => 51
[img] =>
)
[10] => Array
(
[name] => yyy
[url] => http://127.0.0.1/abc/img12.png
[count] => 22
[img] =>
)
[11] => Array
(
[name] => cxz
[url] => http://127.0.0.1/abc/img12.png
[count] => 41
[img] =>
)
[12] => Array
(
[name] => tre
[url] => http://127.0.0.1/abc/img12.png
[count] => 32
[img] =>
)
[13] => Array
(
[name] => fds
[url] => http://127.0.0.1/abc/img12.png
[count] => 10
[img] =>
)
)
array WITHOUT images (field "img" )should always be placed underneath array WITH images. After this there will be sorted on the amount of products (field count) in the array.
Means I have to show sort array first on the basis of img then count.
I am using
usort( $childLinkCats, 'sortempty' );`
function sortempty( $a, $b ) {
return empty( $a['img'] );
}
it will show array with image value above the one who contains null value.
and to sort through count Im using
usort($childLinkCats, "_sortByCount");
function _sortByCount($a, $b) {
return strnatcmp($a['count'], $b['count']);
}
It will short by count
But I am facing problem that only 1 working is working at a time, but I have to use both, please help me.