vbnullchar Posted January 17, 2007 Share Posted January 17, 2007 I need to sort this array by [score].. anyone?[code]Array ( [1] => Array ( [id] => 1 [title] => Acoustic - Pop [score] => 4 ) [2] => Array ( [id] => 2 [title] => Ballad [score] => 1 ) [3] => Array ( [id] => 3 [title] => Bossa Nova [score] => 3 ) [4] => Array ( [id] => 4 [title] => Chillout/Lounge [score] => 2 ) );[/code] Link to comment https://forums.phpfreaks.com/topic/34532-multi-dimentional-array-sort/ Share on other sites More sharing options...
kobmat Posted January 17, 2007 Share Posted January 17, 2007 [CODE]<?php$arr = array(array('id' => 1, 'title'=> 'Acoustic - Pop', 'score'=> 4), array('id' => 2, 'title'=> 'Ballad', 'score'=> 1), array('id' => 3, 'title'=> 'Bossa Nova', 'score'=> 3), array('id' => 4, 'title'=> 'Chillout/Lounge', 'score'=>2));define( 'ASC_AZ', 1000 );define( 'DESC_AZ', 1001 );define( 'ASC_NUM', 1002 );define( 'DESC_NUM', 1003 );function stickysort( $arr, $field, $sort_type, $sticky_fields = array() ) { $i = 0; foreach ($arr as $value) { $is_contiguous = true; if(!empty($grouped_arr)) { $last_value = end($grouped_arr[$i]); if(!($sticky_fields == array())) { foreach ($sticky_fields as $sticky_field) { if ($value[$sticky_field] <> $last_value[$sticky_field]) { $is_contiguous = false; break; } } } } if ($is_contiguous) $grouped_arr[$i][] = $value; else $grouped_arr[++$i][] = $value; } $code = ''; switch($sort_type) { case ASC_AZ: $code .= 'return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);'; break; case DESC_AZ: $code .= 'return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));'; break; case ASC_NUM: $code .= 'return ($a["'.$field.'"] - $b["'.$field.'"]);'; break; case DESC_NUM: $code .= 'return ($b["'.$field.'"] - $a["'.$field.'"]);'; break; } $compare = create_function('$a, $b', $code); foreach($grouped_arr as $grouped_arr_key=>$grouped_arr_value) usort ( $grouped_arr[$grouped_arr_key], $compare ); $arr = array(); foreach($grouped_arr as $grouped_arr_key=>$grouped_arr_value) foreach($grouped_arr[$grouped_arr_key] as $grouped_arr_arr_key=>$grouped_arr_arr_value) $arr[] = $grouped_arr[$grouped_arr_key][$grouped_arr_arr_key]; return $arr;}print_r(stickysort($arr, 'score', ASC_NUM));?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/34532-multi-dimentional-array-sort/#findComment-162702 Share on other sites More sharing options...
vbnullchar Posted January 17, 2007 Author Share Posted January 17, 2007 thanks alot i'll try that hope it work on codes.. Link to comment https://forums.phpfreaks.com/topic/34532-multi-dimentional-array-sort/#findComment-162708 Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 [code]<pre><?php $data = array( array('id' => 1, 'title' => 'Acoustic - Pop', 'score' => 4), array('id' => 2, 'title' => 'Ballad', 'score' => 1), array('id' => 3, 'title' => 'Bossa Nova', 'score' => 3), array('id' => 4, 'title' => 'Chillout/Lounge', 'score' => 2) ); foreach ($data as $index => $array) { $score[$index] = $array['score']; } array_multisort($score, SORT_DESC, SORT_NUMERIC, $data); print_r($data);?></pre>[/code] Link to comment https://forums.phpfreaks.com/topic/34532-multi-dimentional-array-sort/#findComment-162797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.