simonsays Posted August 6, 2006 Share Posted August 6, 2006 The problem is the following. I make a new array from database results:[code]$allpaths[]=array ("id" => $row['id'], "path" => $path);[/code]It is done inside a while loop and the array is ok, except for one thing. I would like to sort the elements by 'path'...So I tried to do it like this:[code]foreach ($allpaths as $key => $row) { $id[$key] = $row['id']; $path[$key] = $row['path'];}array_multisort($path, SORT_ASC, $id, SORT_ASC, $allpaths);[/code]But this does not seem to work and all the time I get [b]Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag [/b] error...The complete function code is here...[code]<?function GetAllPaths($id=NULL){$sql="SELECT id, name, upper_catalog FROM catalog";$getcats=mysql_query($sql) or die (mysql_error());$allpaths=array();while ($row=mysql_fetch_array($getcats)){if ($row['upper_catalog']!=0){$path=Path($row['id']);$allpaths[]=array ("id" => $row['id'], "path" => $path);}else {$allpaths[]= array ("id" => $row['id'], "path" => $row['name']);}}foreach ($allpaths as $key => $row) { $id[$key] = $row['id']; $path[$key] = $row['path'];}array_multisort($path, SORT_ASC, $id, SORT_ASC, $allpaths);return $allpaths;}?>[/code]I would be very grateful if someone could really help me. Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/ Share on other sites More sharing options...
ignace Posted August 6, 2006 Share Posted August 6, 2006 what does your Path() function do? And what does it do with your $row['id']?P.S.: remove the $allpaths from you array_multisort(); Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/#findComment-70215 Share on other sites More sharing options...
king arthur Posted August 6, 2006 Share Posted August 6, 2006 Doesn't look like you need array_multisort() for this, can you not just use usort()? Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/#findComment-70218 Share on other sites More sharing options...
simonsays Posted August 6, 2006 Author Share Posted August 6, 2006 [quote author=ignace link=topic=103171.msg410692#msg410692 date=1154875181]what does your Path() function do? And what does it do with your $row['id']?P.S.: remove the $allpaths from you array_multisort();[/quote]Path function forms a complete category path to some lower category (e.g. Monitors>CRT, Printers> Laser > Epson, etc.)Here it is [code]function Path($child){$cat=getParents($child);$path=getCatName($child);foreach ($cat as $element){$path = $element['n'].' > '.$path ;}return $path;}[/code]I removed $allsorts, but it didn't seem to help much.[quote author=king arthur link=topic=103171.msg410695#msg410695 date=1154875516]Doesn't look like you need array_multisort() for this, can you not just use usort()?[/quote]array_multisort() seemed to be less complicated for me. do you think usort() is easier to use? Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/#findComment-70240 Share on other sites More sharing options...
king arthur Posted August 6, 2006 Share Posted August 6, 2006 Well from the page in the manual from this very site:[quote]The argument structure of this function is a bit unusual, but flexible. The first argument has to be an array. Subsequently, each argument can be either an array or a sorting flag from the following lists. [/quote]so, although I've never used it, it would seem array_multisort() isn't quite going to do what you need. Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/#findComment-70243 Share on other sites More sharing options...
simonsays Posted August 6, 2006 Author Share Posted August 6, 2006 the code that I took for array_multisort comes directly from php.net Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/#findComment-70257 Share on other sites More sharing options...
freshwebs Posted August 6, 2006 Share Posted August 6, 2006 You should use usort. Link to comment https://forums.phpfreaks.com/topic/16704-sorting-associative-array-trouble/#findComment-70302 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.