Help sorting an object array

Help me sort $item[]. I have been trying whole day but do not seem to find the right way.
[php]class Item {

  private $name = "";
  private $itemValues = 0.00;
  public $price = 0.00;
  public $quantity = 1; 

  // constructor with default values (used when parameters not passed)

  public function __construct($name="unknown",$itemValue= 0.00)  {
     $this->name = $name;
     $this->itemValue = $itemValue;
  }

  // accessors and mutators
  function getName() { return $this -> name; }

  function getItemValues() { return $this -> itemvalues; }

  function setName($name) { $this -> name = $name; }

  function setItemValue($itemValue) { $this -> itemValues = $itemValues;}

function printProperties() {
	print ("For name ".$this->name.", the itemValue is ".$this->itemValue."<br/><br/>");
  }

}

print(" SlotCar Report");

function printTitle ( ) { // print heading line

 print '<th><th> Name </th><th><th> ItemValues </th></tr>
 <br/><br/>';

}

function totalItemValues ( &$sumValues ) { // pass array as reference parm

 $sum = 0.00;

 foreach ($sumValues as $c)  {

   $sum += $c;

 }

 return $sum;

}

function printFooting ($totalItemValues) { // print a report footing line

 print("<tr><th colspan=2 align=\"right\"><br/> Total of Item Value:</th>
     <td align=\"right\"><br/>$totalItemValues</td></tr>");

}

// main
$fn = “slotcar.csv”;
$fp = fopen($fn, “r”);
$delimiter = ‘,’; // field delimiter
$itemValues = array( ); // optional, for documentation
$totalItemValues = 0.00;

print("

");
printTitle( ); // call “void” function

while ($item = fgetcsv($fp, $delimiter)) {

 $id = $item[0];
 $name = $item[1];
 $type = $item[2];
 $price = $item[3];
 $quantity = $item[4];
 $sumValues = $quantity * $price;   // add cost from current record to array
 $totalItemValues += $sumValues;
 
 [size=10pt][u][i][b]items[] =new Item($id, sumValues)[/b][/i][/u][/size]
 print("</td><td><td>$name </td><td><td>  $sumValues </td></tr>");

}

printFooting ($totalItemValues);
print("

");
fclose($fp);[/php]

?>

have you tried using ‘order by’ ?

$items is an array of Item objects, and you want to sort this array?

Depending on what and how you want to sort, give each item a key to sort by.

$items[key] = new Item(…);

Then you can ksort($items) or krsort($items)

Sponsor our Newsletter | Privacy Policy | Terms of Service