// http://stackoverflow.com/a/2699110/1163000
function sort_array_by_value($key, &$array) {

$sorter = array();
$ret = array();

reset($array);

foreach($array as $ii => $value) {
$sorter[$ii] = $value[$key];
}

asort($sorter);

foreach($sorter as $ii => $value) {
$ret[$ii] = $array[$ii];
}

$array = $ret;

}

Contoh Kasus

Sampel array:

$my_array = array(
array(
'id' => 1,
'title' => 'Orange',
'time' => '2014-03-05 03:00:42'
),
array(
'id' => 2,
'title' => 'Banana',
'time' => '2013-01-06 13:05:15'
),
array(
'id' => 3,
'title' => 'Grape',
'time' => '2011-05-15 01:30:45'
)
);

Menyortir data berdasarkan nilai dari title

sort_array_by_value('title', $my_array);

print_r($my_array);

Akan menghasilkan urutan seperti ini:

Array
(
[1] => Array
(
[id] => 2
[title] => Banana
[time] => 2013-01-06 13:05:15
)

[2] => Array
(
[id] => 3
[title] => Grape
[time] => 2011-05-15 01:30:45
)

[0] => Array
(
[id] => 1
[title] => Orange
[time] => 2014-03-05 03:00:42
)

)
Axact

Axact

Vestibulum bibendum felis sit amet dolor auctor molestie. In dignissim eget nibh id dapibus. Fusce et suscipit orci. Aliquam sit amet urna lorem. Duis eu imperdiet nunc, non imperdiet libero.

Post A Comment:

0 comments: