astralaaron Posted December 11, 2013 Share Posted December 11, 2013 (edited) EDIT: mysqli_multi_query() to array to sort I have a few tables that I want to pull into one query result. I can get a multi array with the following code: public function getPosts( ) { $data= array(); $queries =" SELECT * FROM `imagePosts`; "; $queries .= " SELECT * FROM `messages`; "; $queries .= " SELECT * FROM `videos`; "; $exe = parent::database()->multi_query($queries); if (!$exe) { printf("Error: %sn", parent::database()->error); return false; } do { if ($result = parent::database()->store_result()) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $data[] = $row; } $result->free(); } if (!parent::database()->more_results()) { break; } } while (parent::database()->next_result()); return $data; } There are a few examples of sorting arrays with usort() that I found, but for some reason I just cannot understand how that function works... There is a datetime field in my tables that I want to sort by which should mix the table results together.. Does anyone understand how to sort an array by a date field? Edited December 11, 2013 by astralaaron Link to comment Share on other sites More sharing options...
astralaaron Posted December 11, 2013 Author Share Posted December 11, 2013 So, the following works, but I can't figure out how to get it to work within my class files function date_compare($a, ${ $t1 = strtotime($a['when']); $t2 = strtotime($b['when']); return $t1 - $t2;}usort($array, 'date_compare'); the way usort takes in a string as a function name is throwing me off. How do I pass date_compare() as member of my class instead of just a function? I want to be able to do this sorting within the getPosts function instead of procedurally after the result is returned.. I thought this would work within my class but no luck: usort($array, function ($a, ${ $t1 = strtotime($a['when']); $t2 = strtotime($b['when']); return $t1 - $t2;}); any ideas? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 11, 2013 Share Posted December 11, 2013 If you're passing an object method then you use an array to specify the object, then the method name: usort($array, array($object, 'method')); Note that if you're using PHP 5.4, Javascript-style short array syntax works also: usort($array, [$this, 'method']); Link to comment Share on other sites More sharing options...
astralaaron Posted December 11, 2013 Author Share Posted December 11, 2013 perfect! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now