note 95754 added to function.array-multisort

From: Date: Wed, 20 Jan 2010 11:42:28 +0000
Subject: note 95754 added to function.array-multisort
Groups: php.notes 
Request: Send a blank email to php-notes+get-165132@lists.php.net to get a copy of this message
array_multisort works normally in php 5.3, but it forces arguments to be references. It doesn't make differences for common array_multisort() usage, but makes "problems" for sorting variable number of arrays where call_user_func_array() function is involved. So all sorting arrays have to be collected into new one as a references to array variables: <?php $sortArgs = array(); for (...) { ... $sortArgs[] = &$valuesArray; ... } call_user_func_array('array_multisort', $sortArgs); ?> This (requiring arguments to be a reference) is not actually a problem since source array will not be sorted otherwise. Important note! Don't forget to destroy $valuesArray variable if you use it over each array_multisort() argument processing iteration. If you don't do it, all array_multisort() arguments will contain the same array: <?php for (...) { ... $sortArgs[] = &$valuesArray; unset($valuesArray); ... } ?> And the last important thing :) Collect sorting arrays somewhere. PHP 5.3 will transfer reference into value (when $valuesArray is destroyed) and you will get "Parameter 1 to array_multisort() expected to be a reference, value given" warning again otherwise. Final code should look like this: <?php $sortArgs = array(); $sortFieldValues = array(); for (...) { ... $sortFieldValues[] = &$valuesArray; $sortArgs[] = &$valuesArray; unset($valuesArray); ... } call_user_func_array('array_multisort', $sortArgs); ?> ---- Server IP: 195.54.192.44 Probable Submitter: 89.110.59.121 ---- Manual Page -- http://www.php.net/manual/en/function.array-multisort.php Edit -- https://master.php.net/note/edit/95754 Del: integrated -- https://master.php.net/note/delete/95754/integrated Del: useless -- https://master.php.net/note/delete/95754/useless Del: bad code -- https://master.php.net/note/delete/95754/bad+code Del: spam -- https://master.php.net/note/delete/95754/spam Del: non-english -- https://master.php.net/note/delete/95754/non-english Del: in docs -- https://master.php.net/note/delete/95754/in+docs Del: other reasons-- https://master.php.net/note/delete/95754 Reject -- https://master.php.net/note/reject/95754 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165132) next »