Home · Latest · Trends  /  PHP
15

Sort (Order) foreach results in Smarty

rated 15 times [  15] [ 0]  / comments: 1 / hits: 2259  / 3 years ago, sat, sep 10, 2016, 15:07:57
Smarty sort foreach results in Smarty file
// libs/plugins/modifier.sortby.php:

<?php 
# sorts an array of named arrays by the supplied fields 
# code by dholmes at jccc d0t net 
# taken from http://au.php.net/function.uasort 
# modified by cablehead, messju and pscs at http://www.phpinsider.com/smarty-forum 
function array_sort_by_fields(&$data, $sortby){ 
static $sort_funcs = array(); 
if (empty($sort_funcs[$sortby])) 
{ 
$code = "$c=0;"; 
foreach (explode(',', $sortby) as $key) 
{ 
$d = '1'; 
if (substr($key, 0, 1) == '-') 
{ 
$d = '-1'; 
$key = substr($key, 1); 
} 
if (substr($key, 0, 1) == '#') 
{ 
$key = substr($key, 1); 
$code .= "if ( ( $c = ($a['$key'] - $b['$key'])) != 0 ) return $d * $c;n"; 
} 
else 
{ 
$code .= "if ( ($c = strcasecmp($a['$key'],$b['$key'])) != 0 ) return $d * $c;n"; 
} 
} 
$code .= 'return $c;'; 
$sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code); 
} 
else 
{ 
$sort_func = $sort_funcs[$sortby]; 
} 
@uasort($data, $sort_func); 
} 
# 
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field 
# 
function smarty_modifier_sortby($arrData,$sortfields) { 
array_sort_by_fields($arrData,$sortfields); 
return $arrData; 
} 
?>

// Usage:

{foreach from=$results|@sortby:"itemid" item=foo}
{$foo.text}
{/foreach}
More From » PHP
 

Comments

2
For PHP 7.2.
Code:
<?php
# sorts an array of named arrays by the supplied fields
function array_sort_by_fields(&$data, $sortby) {
 static $sort_funcs = array();
 if (empty($sort_funcs[$sortby])) {
  $code = "\$c=0;";
  foreach (explode(',', $sortby) as $key) {
   $d = '1';
   if (substr($key, 0, 1) == '-') {
    $d = '-1';
    $key = substr($key, 1);
   }
   if (substr($key, 0, 1) == '#') {
    $key = substr($key, 1);
    $code .= "if ( ( \$c = (\$a['$key'] - \$b['$key'])) != 0 ) return $d * \$c;\n";
   } else {
    $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
   }
  }
  $code .= 'return $c;';
  $sort_func = $sort_funcs[$sortby] = function ($a, $b) {
   return $code;
  }
  ;
 } else {
  $sort_func = $sort_funcs[$sortby];
 }
 @uasort($data, $sort_func);
}
#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#
function smarty_modifier_sortby($arrData, $sortfields) {
 array_sort_by_fields($arrData, $sortfields);
 return $arrData;
}
?>
[#10] Wednesday, September 12, 2018, 15:02:45
 
my3profile
commented 6 months ago
Only authorized users can post. Please sign in first, or register a free account
 
Share
Snippets
Validate domain name with PHP gethostbyname
3 years ago, sat, sep 10, 2016
List all files in Zip archive with PHP
3 years ago, sat, sep 10, 2016
How to get the current url in smarty file
3 years ago, sat, sep 10, 2016
Grabbing a Favicon from external website
2 years ago, tue, feb 21, 2017
Posted
Michael

Michael

Member since Sep 10, 2016
Total Code Snippets: 5
Total Comments: 0
Location: n/a
Following
User not following anyone yet.