Jump to content

Sorting list/array


phpnewone

Recommended Posts

Hello.  I am just trying to sort an array alphabetically.  Here's the code I'm using:

 

function GetAssocCharities($registrationNo)
{
	$db = ConnectionString::ConnectTo_www_ago_mo_gov_data();
	$sqlstring = "SELECT * FROM charityPartners where RegistrationNo LIKE '$registrationNo' ORDER BY Name";


	$results = @mysql_query($sqlstring);

	$org = null;


	while ($resultArray = mysql_fetch_array($results))
	{

		$temp = new Registrants($resultArray['ARegNoFormatted'], $resultArray);

		$org[] = $temp; 
	}

	return asort($org);

}

 

When I try to loop through the array using a foreach loop, I get an error that Invalid argument supplied for foreach().

 

Here's my foreach loop:

$lstCharities = OrgQueries::GetAssocCharities($registrant->RegNoFormatted);

foreach($lstCharities as $c)
		{

//dom something here
}

 

I'm sure its something with not understanding list v array or something or that nature, but I'm not having much luck figuring it out with google.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/219917-sorting-listarray/
Share on other sites

The sorting functions return true or false, depending on success or failure. So instead of returning an array you are now either returning true or false, hence why it is throwing the error at you. You simply call asort() then return the variable normally.

 

asort($org);
return $org;

 

Always make sure you check the manual my friend. :)

Link to comment
https://forums.phpfreaks.com/topic/219917-sorting-listarray/#findComment-1140262
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.