简体   繁体   中英

Sorting Array of new class items based on class->value for PHP

I have an array of items

class Item{
   public $username = "";
   public $id =""
   public function __construct($id,$user){
     if($input != "")
       $this->id = $id;
     if($username !="")
       $this->username = $user;
   }
}

I am grabbing some json data that is already parsed (correctly). And I am taking the Json and making a new Item($userid, $username); for every returned item And then I want to sort by the Users id so I have the following.

$ItemArray = array();
foreach($jsonData as $data){
  $username = $data['username'];
  $id = $data['id'];
  $ItemArray = new Item($id,$username);
}

Now the issue I have is that I am trying to get the $id and organize it by High to Low number id.

If I make a single array of the $id s then the following code will work. However if I use my actual variables it won't work.

function cmp($a, $b) {
  if($a == $b) 
     return 0;
  else 
     return $a < $b ? 1 : - 1; // reverse order
}
usort($array, "cmp");

(This isn't my real code, I am taking my actual problem and simplifying everything. I actually have several hundred lines of code for the actual program. I just need a simple representation of my problem for this because I can't get my stuff to work.

So what I had to do is more checking to make sure every object that passed through was the object type it was suppose to be. I used a bubble sort type of algorithm because I have it set to return the sorted array. Then I loop through each value. The following is the bubbleSort function

function bubbleSort($array) {
 if(! $length = count($array)) {
  return $array;
 }

 for($outer = 0; $outer < $length; $outer ++) {
  for($inner = 0; $inner < $length; $inner ++) {
   if(is_object($array[$outer]) && is_object($array[$inner])) {
    if($array[$outer]->id < $array[$inner]->id) {
     $tmp = $array[$outer];
     $array[$outer] = $array[$inner];
     $array[$inner] = $tmp;
    }
   }
  }
 }
 return $array;
}

I quickly realized I needed a way to contain all the posts on a global scale. So at the very top of the .php page I have the following class.

class Itemarray{
    private $items = "";
    public function getItems(){ if(isset($items){return $$items;}}
    public function setItems($item){
        $this-$items[] = $item;
    }
}
$ItemArray = new Itemarray();

When the $items are created in the loop in my original post so I updated the foreach loop to the following

foreach($jsonData as $data){
  $username = $data['username'];
  $id = $data['id'];
  $ItemArray->setItems(new Item($id,$username));
}

Then next part is how I looped through each array and print out the desired data.

echo '<table>';
echo '    <tr>'; 
echo '       <td>The User IDs are:</td>';
echo '       <td>The Usernames are:</td>';
echo '    </tr>';

foreach(bubbleSort($ItemArray->getItems()) as $item) {
    echo '<tr>'; 
    echo '    <td>'.$item->id.'</td>';
    echo '    <td>'.$item->username.'</td>';
    echo '</tr>';
}
echo '</table>';

Please note that this solution is designed for my personal problem, and my not work for your solution.

Why such a complicated method, writing your own sort algorithm, when you can easily use usort() with an array of objects:

function cmp($a, $b) {
  if($a->id == $b->id) 
     return 0;
  else 
     return $a->id < $b->id ? 1 : -1;
}
usort($array, "cmp");

To sort by id

Or even push each item onto an SPLHeap as you extract it from json, with a compare() method similar to the above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM