- This topic is empty.
-
AuthorPosts
-
January 27, 2015 at 10:32 am #194355justdanParticipant
Hi guys. I’m working on a little PHP function that will sort an array based off of if the item returned is “verified” or not. I’m not very familiar with PHP and I’ve been doing a lot of reading up since I figured there could possibly be a method to make this easier. Reading around it sounded like the uasort() was what I needed for its sorting abilities and that it maintains index association, but I’m a little confused as how to properly use it. The original block of PHP I am working on looks like this:
`<?php
if (count($results)):$lexisIndex = 1; foreach ($results as $person){ $person->isVerified = (substr($person->first_name, 0, 1) == substr($fname, 0, 1) && $person->last_name == $lname ); $person->lexisIndex = $lexisIndex++; } foreach ($results as $person):
?>
`From here I added the sortIt function, put the results into an array, and then sort from that array like this:
`<?php
if (count($results)):$lexisIndex = 1; foreach ($results as $person){ $person->isVerified = (substr($person->first_name, 0, 1) == substr($fname, 0, 1) && $person->last_name == $lname ); $person->lexisIndex = $lexisIndex++; } function sortIt($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } $array = array($results); uasort($array, 'sortIt'); foreach ($results as $person):
?>
`Where I’m stumped is that part where I can get the function to compare the $a and $b to see if each person “isVerified” or not. If they are, I would like these people to display at the top of the list. Any help I can get to give me a nudge in the right direction is always appreciated. Thanks guys.
January 27, 2015 at 1:20 pm #194369AnonymousInactive$a
and$b
are values from the array. In this case, person objects.isVerified
is either true or false, right?function sortIt($a, $b) { return $a->isVerified - $b->isVerified; }
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.