https://cdn.statically.io/gh/dte-project/a/1e23173799f8ac7c346f345f20de4e5ccb246b1e/shell.v2.min.css
Loading...

Minggu, 09 Maret 2014

// 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
)

)

View more articles

Keyword link

Subscribe via Email

Sign up by email to get the latest news from us..

Contact us

Send a message to us.

Template Hasil Cloning II | Copyright 2015 - 2016 | Rip Code by Shn | ShannenPio Cloning