Jan Walraven
DevOps



Categories:
  • Arduino (2)
  • CSS (3)
  • Docker (10)
  • ESXi (1)
  • Git (4)
  • Google Cloud (2)
  • Javascript (6)
  • Linux (31)
  • Mac (6)
  • Magento (4)
  • Mysql (13)
  • PHP (9)
  • Zend framework 2 (9)
  • Posted on April 26, 2012
    
    $products = array( array( name=>"A", price=>4.5  ),
                       array( name=>"C", price=>5.5  ),
                       array( name=>"D", price=>2.5  ),
                       array( name=>"B", price=>2.5  )
    );
     
    function priceCmp( $a, $b ){
        if ( $a[price] == $b[price] )
            return 0;
        if ( $a[price] < $b[price] )
             return -1;
        return 1;
    }
    usort( $products, priceCmp );
    foreach ( $products as $val )
         print "$val[name]: $val[price]
    n";

    Sorting within an class:

    
    class example{
            private $data_array_sort;
           
            public function sort_array($arr,$sort){
                    $this->data_array_sort = $sort;
                    usort($arr,array($this,"getRecordsSort"));
            }
           
            private function getRecordsSort($a,$b){
                    if ( $a[$this->data_array_sort] == $b[$this->data_array_sort] ){
                            return 0;
                    }
                    if ( $a[$this->data_array_sort] < $b[$this->data_array_sort] )
                            return -1;
                    return 1;
            }
    }