note 29340 modified in function.array-multisort by vrana

From: Date: Tue, 17 Aug 2004 13:59:16 +0000
Subject: note 29340 modified in function.array-multisort by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79972@lists.php.net to get a copy of this message
A general function for sorting a multi-dimensional array by a specific column and order (assuming that all rows of the array have the same indexes (columns)): <?php function array_csort(&$results, $column=0, $order=SORT_ASC) { $keys = array_keys($arr[0]); foreach ($results as $row) $sortarr[] = $row[$keys[$column]]; array_multisort(&$sortarr, $order, &$results, $order); } // array_csort ?> Note that the array parameters to multisort must be passed by reference! This eluded me for a long time... --was-- A general function for sorting a multi-dimensional array by a specific column and order (assuming that all rows of the array have the same indexes (columns)): function array_csort(&$results, $column=0, $order=SORT_ASC) { $keys = array_keys($arr[0]); foreach ($results as $row) $sortarr[] = $row[$keys[$column]]; array_multisort(&$sortarr, $order, &$results, $order); } // array_csort Note that the array parameters to multisort must be passed by reference! This eluded me for a long time... http://php.net/manual/en/function.array-multisort.php

« previous php.notes (#79972) next »