Jump to content

[SOLVED] PHP ARRAYS


enkidu72

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/136059-solved-php-arrays/
Share on other sites

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 ...

 

 

Link to comment
https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709440
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709457
Share on other sites

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);  
}
?>

Link to comment
https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709464
Share on other sites

<?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);
?>

Link to comment
https://forums.phpfreaks.com/topic/136059-solved-php-arrays/#findComment-709479
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.