mb1 Posted September 18, 2011 Share Posted September 18, 2011 hi, i am having issues with sorting within a conditional statement. please see the code below. right now, i ve commented out the sorting below. basically i want to sort by: $venue1->hereNow->count. I am managing to pull the data correctly. see: http://marineboudeau.com/lab/4sq/ <?php if (is_object($venue->response)) { //sort($venue->response->groups[0]->items->hereNow->count); foreach ($venue->response->groups[0]->items as $venue1) { if ($venue1->hereNow->count != '0'){ //var_dump ($venue1); echo "<div style='font-weight:bold;'>".$venue1->name."</div>"; echo "<div>".$venue1->location->address." ".$venue1->location->city."</div>"; echo "<div>".$venue1->hereNow->count."</div>"; echo "</br>"; } } } ?> any idea what i need to do to make it happen? thank you. marine Quote Link to comment https://forums.phpfreaks.com/topic/247353-need-sorting-in-foreach-if-statement-cant-make-it-happen/ Share on other sites More sharing options...
creata.physics Posted September 18, 2011 Share Posted September 18, 2011 You said you want to sort it by $venu1->hereNow->count, as far as I know, we won't be able to sort the array by count unless you assign count to an array key or value. as far as i know php's sorting functions are for arrays only. We can rebuild the array with count as the key and assign that way. That's only if doing: sort($venue->response->groups[0]); doesn't do what you want. You'll need to assign count to an array key and sort that way, you can do that with this code below. <?php $new_array = array(); if (is_object($venue->response)) { foreach ($venue->response->groups[0]->items as $venue1) { $new_array[$venu->response->groups[0]->items->count] = $venue1; } sort($new_array); foreach( $new_array as $venue1 ) { if ($venue1->hereNow->count != '0') { //var_dump ($venue1); echo "<div style='font-weight:bold;'>".$venue1->name."</div>"; echo "<div>".$venue1->location->address." ".$venue1->location->city."</div>"; echo "<div>".$venue1->hereNow->count."</div>"; echo "</br>"; } } } ?> Note that the code may have some typos and make not work directly, but it should give you an idea of how to make a new array with (hopefully) the assortment you want. Quote Link to comment https://forums.phpfreaks.com/topic/247353-need-sorting-in-foreach-if-statement-cant-make-it-happen/#findComment-1270321 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.