Jump to content

array_multisort problem


gholland85

Recommended Posts

Hello, I'm relatively new to PHP so I'm sure this will be a quick fix for a guru out there.

 

I have spent some time reading over the manual at: http://php.net/manual/en/function.array-multisort.php but I can't seem to get it to work for me.

 

I have a website which connects to a third party web service and returns a list of available accommodations. I'm trying to sort this list alphabetically by the name of the property. I'm using Smarty template engine so things are slightly different but array_multisort should still work the exact same in the .php file.

 

In my .tpl file for the results I have the following code:

 

{if is_array($params.RESULTS.PROPERTY)}

{foreach key=key item=val from=$params.RESULTS.PROPERTY}


{if $val.TEXT1 != ''}
<div class="resultsPromo">
{else}
<div class="results">
{/if}

	<a href="../{$val.LINK}"><img src="/propertyThumbs/{$val.IMAGE}" alt="{$val.NAME}" width="195" height="110" border="0" alt="" class="photo" /></a>

	<div class="details">
		<span class="toplineleft"><img src="/images/stars/{$val.STAR}.gif" width="80" height="16" border="0" alt="" class="star" /> <a href="../{$val.LINK}" class="name">{$val.NAMEONLY}</a></span>{if $val.TEXT1 != ''}<span class="toplineright">{include file="`$phpRootTemplates`/blackoutdates.tpl"}</span><div class="clear"></div>{else}{/if}	
		<div class="description">
		<p><strong>Location:</strong> {$val.LOCATION}
		<span class="right"><strong>Distance to Lifts:</strong> {if $val.LIFTDISTANCE < 1} Ski-in Ski-out {elseif $val.LIFTDISTANCE < 100} {$val.LIFTDISTANCE} minute walk {elseif $val.LIFTDISTANCE == 110} 10 minute drive {else} {$val.LIFTDISTANCE|substr:"2"} minute drive{/if}</span></p>
		<p>{$val.DESCRIPTION}</p>
		</div>
		<div class="rate">Average Rate<br/><span class="priceLarge">${$val.LOWESTPRICE}</span> <span style="color:#595959;">/nt</span></div>
		<div class="clear"></div>

	</div>
	<div class="clear"></div>

</div>

{/foreach}

 

 

Then in the PHP file I have this to try and sort it but unfortunately I don't think I'm even close.

 

if($params["RESULTS"]["PROPERTY"]){

foreach($params["RESULTS"]["PROPERTY"] as $key => $val) {
	$column[$key] = $val["NAME"];
}

//Sort the array either asc or desc depending on the column
array_multisort($column, SORT_ASC, SORT_STRING,$params["RESULTS"]["PROPERTY"]);
}

 

If anyone can point me in the right direction it would be very much appreciated.

 

Thankyou!

 

Link to comment
https://forums.phpfreaks.com/topic/188988-array_multisort-problem/
Share on other sites

I have spent some time reading over the manual at: http://php.net/manual/en/function.array-multisort.php but I can't seem to get it to work for me.

 

In what way does it not work?  Does it mangle the results or does it just not sort in the right order?

Ok I've verified that the way you are using array_multisort() should work.  I would debug it as follows:

 

1.  Check that the sorting code is actually being executed, by adding a print statement

2.  Check that the array is sorted after the sorting code is executed.  Can use var_dump() for this, for example:

print "<pre>"; var_dump($params["RESULTS"]["PROPERTY"]); print "</pre>";

3.  If it's still not working, dump the array contents just before it goes into the smarty template.  Is it sorted there?

 

That should give you enough information to either pinpoint the problem or at least narrow down where to look.

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.