communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "array handling" - download "array multi sort" - open demo of "array multi sort"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

array handling - array multi sort was created/last modified on Tue 29 Jun 2004 6:07 PM

sort multidimensional arrays by field specified.
specify the key by which you want your multiarray sorted.

<?php

// demo array to sort
$latest_array = array(
  array(
'news','1234567890','sdf','asdpojq'),
  array(
'news','1234567892','uruy','xbuqiwpo'),
  array(
'comment','1234567893','fghj','mjktyu'),
  array(
'article','1234567891','cvmo','pjklgg'),
  array(
'news','1234567894','qwenb','asbhtyhj'),
);

$sort_field = 3; // enter the number of field to sort

// compare function
function cmpi($a, $b)
{
     global
$sort_field;
     return
strcmp($a[$sort_field], $b[$sort_field]);
}

// do the array sorting
usort($latest_array, 'cmpi');

// demo output
echo '<pre>';
print_r($latest_array);
echo
'</pre>';

?>

learn more about some of the PHP functions used in this snippet: array, echo, function, glob, global, key, print, print_r, return, sort, strcmp, usort

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari