damian0612 Posted April 25, 2009 Share Posted April 25, 2009 How can I reverse the results of an array in php? I have queried mysql: $result = mysql_query(" SELECT * FROM fixtures WHERE result != '' ORDER BY date DESC LIMIT 6", $connection); Now, I want to reverse the results? I can't do it in the query as it will return many rows if I didn't limit it and these 6 are the ones I need, therefore if I ordered by ASC it would get a different 6 which I dont want. I have tried: foreach (array_reverse($result) as $result2) { print "$result2"; But I get errors. Can anyone help? Thanks Damian Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/ Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 array_reverse <3 Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819045 Share on other sites More sharing options...
damian0612 Posted April 25, 2009 Author Share Posted April 25, 2009 array_reverse As u can see from the code I posted above I have tried to apply this however have been unsucessful, thus this post ??? Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819075 Share on other sites More sharing options...
thebadbad Posted April 25, 2009 Share Posted April 25, 2009 $reversed_array = array_reverse($result); And then use $reversed_array in your foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819079 Share on other sites More sharing options...
DarkWater Posted April 25, 2009 Share Posted April 25, 2009 That code that you posted in the original post should work. Which "errors" do you get? Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819080 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 You need to actually get the results first. You only have the resource returned by mysql_query. See: mysql_fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819097 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 try that <?php $result = mysql_query("SELECT * FROM fixtures WHERE result != '' ORDER BY date DESC LIMIT 6", $connection); $results = array(); for ($i = (mysql_num_rows($result) - 1); $i > 0; $i--) { mysql_data_seek($result,$i); $results[] = mysql_fetch_assoc($result); } ?> note: its untested.. as I just wrote it, but if it doesn't work just tweak it a bit, thats along the lines of what you need Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819117 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 mysql_fetch_*() moves the pointer one position forward automatically, so you would have to decrement by 2 on each iteration instead. Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819150 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 daniel, actually, I'm decreasing the $i variable in each loop of the for, whether it is 7 or 8 when I go to set it to 6, it will still get set to 6 Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819151 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 Right, sorry yeah, that's my fault. I was thinking for a moment that $i was the cursor, which it of course isn't. You're right Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819152 Share on other sites More sharing options...
.josh Posted April 25, 2009 Share Posted April 25, 2009 Think I'd rather stick with a regular while loop and array_reverse afterwards. Seems a lot more cleaner/clearer in intention if reading, imo. Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819153 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 Think I'd rather stick with a regular while loop and array_reverse afterwards. Seems a lot more cleaner/clearer in intention if reading, imo. I know, thats what I was originally going to formulate for him, however, using the while loop, then array reverse, will ofcourse do the same operations double, instead my way, you get the data, and put it in the right place, at the same time Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819160 Share on other sites More sharing options...
.josh Posted April 25, 2009 Share Posted April 25, 2009 yeah I'm not arguing that ^^ is probably more efficient on the processor...but I think this is one of those cases if I were coding it, I'd sacrifice that smidgen of efficiency for readability. edit: I mean, we are talking about a 6 row query... Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819161 Share on other sites More sharing options...
damian0612 Posted April 25, 2009 Author Share Posted April 25, 2009 Thanks for your help, got it solved: //temporary results array $results = array(); //our query $sql = "SELECT * FROM fixtures WHERE result != '' ORDER BY date DESC LIMIT 6"; $query = mysql_query($sql); while($result = mysql_fetch_assoc($query)){ //store the results in an array $results[] = $result; } //reverse our array $fixtures = array_reverse($results); //print our array foreach($fixtures as $fixture){ print_r($fixture); } Quote Link to comment https://forums.phpfreaks.com/topic/155617-solved-reversing-an-array/#findComment-819199 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.