Jump to content

Need sorting in foreach / if statement. can't make it happen


mb1

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.