enkidu72 Posted December 8, 2008 Share Posted December 8, 2008 Hi all , I need to put values in an array , an url and a weight . And then sort all the elements in the array by weight . es.: url1 - weight 5 url2 - weight 4 url3 -weight 7 and then have them sorted more or less this way : url2 - weight4 url1 -weight5 url3 -weight7 ... I've tried this but i think I'm quite confused on something <?php $Menu=array(); function cmp($x, $y) { if ($x[1] == $y[1]) { return 0; } return ($x[1] > $y[1]) ? 1 : -1; } $titolo1="primo titolo"; $titolo2="secondo titolo"; $titolo3="terzo titolo"; $titolo1_peso="1"; $titolo2_peso="3"; $titolo3_peso="2"; //array_push($Menu,$titolo1); //array_push($Menu,$titolo2); //array_push($Menu,$titolo3); $Menu[]=$titolo1; $Menu[]=$titolo2; $Menu[]=$titolo3; $Menu["$titolo1"]=$titolo1_peso; $Menu["$titolo2"]=$titolo2_peso; $Menu["$titolo3"]=$titolo3_peso; usort($Menu,'cmp'); foreach ($Menu as $key => $value){ print "$key -- $value \n"; } ?> Can someone please help ? Many thx in advance David Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/ Share on other sites More sharing options...
The Little Guy Posted December 8, 2008 Share Posted December 8, 2008 Give this a try: $a = array('weight 5','weight 4','weight 7'); sort($a); for($i = 0;$i<count($a);$i++){ $a[$i] = str_replace(' ',$a[$i]); } Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709428 Share on other sites More sharing options...
enkidu72 Posted December 8, 2008 Author Share Posted December 8, 2008 I think I wrote it wrong ... The array should be something similar ... Array[0] { Array[0] { id => 1 titolo => titolo1 weight => 2 } Array[1] { id => 2 titolo => titolo2 weight => 1 } } And then sorted by weight ... Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709440 Share on other sites More sharing options...
The Little Guy Posted December 8, 2008 Share Posted December 8, 2008 $a = array('weight' => 5,'weight' => 4,'weight' => 7); $n = array(); foreach($a[0][3] as $v1 => $v2){ $n[] = $v1.$v2; } print_r($n); Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709449 Share on other sites More sharing options...
sasa Posted December 8, 2008 Share Posted December 8, 2008 try <?php $test = array( array('id' => 1, 'titolo' => 'titolo1', 'weight' => 22), array('id' => 2, 'titolo' => 'titolo2', 'weight' => 1), array('id' => 3, 'titolo' => 'titolo3', 'weight' => 21) ); function my_comp($a, $b){return $a['weight'] - $b['weight'];} usort($test, 'my_comp'); print_r($test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709457 Share on other sites More sharing options...
Maq Posted December 8, 2008 Share Posted December 8, 2008 Found this little function online that can be used with any associative array and can sort by any dimension... I already applied it to your scenario: (if your arrays are really big you can pass by reference to save memory) $Menu=array(); $Menu = array( array( "id" => 1, "titolo" => "titolo1", "weight" => 2), array( "id" => 2, "titolo" => "titolo2", "weight" => 1), array( "id" => 3, "titolo" => "titolo3", "weight" => 3) ); cmp($Menu, "weight"); function cmp($arr, $key){ //we loop through the array and fetch a value that we store in tmp for($i = 0; $i $tmp = $arr[$i]; $pos = false; //we check if the key we want to sort by is a string $str = is_numeric($tmp[$key]); if(!$str){ //we loop the array again to compare against the temp value we have for($j = $i; $j if(StringManip::is_date($tmp[$key])){ if(StringManip::compareDates($arr[$j][$key], $tmp[$key], $type = 'asc')){ $tmp = $arr[$j]; $pos = $j; } //we string compare, if the string is "smaller" it will be assigned to the temp value }else if(strcasecmp($arr[$j][$key], $tmp[$key]) $tmp = $arr[$j]; $pos = $j; } } }else{ for($j = $i; $j if($arr[$j][$key] $tmp = $arr[$j]; $pos = $j; } } } if($pos !== false){ $arr[$pos] = $arr[$i]; $arr[$i] = $tmp; } } print_r($arr); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709464 Share on other sites More sharing options...
The Little Guy Posted December 8, 2008 Share Posted December 8, 2008 <?php $a = array( array('id' => 1, 'titolo' => 'titolo1', 'weight' => 22), array('id' => 2, 'titolo' => 'titolo2', 'weight' => 1), array('id' => 3, 'titolo' => 'titolo3', 'weight' => 21), array('id' => 4, 'titolo' => 'titolo4', 'weight' => 258), array('id' => 5, 'titolo' => 'titolo5', 'weight' => 277) ); $n = array(); for($i=0;$i<count($a);$i++){ $n[] = 'weight'.$a[$i]['weight']; } sort($n); print_r($n); ?> Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709479 Share on other sites More sharing options...
enkidu72 Posted December 8, 2008 Author Share Posted December 8, 2008 Many thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-710016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.