Jump to content


Photo

sorting associative array - trouble


  • Please log in to reply
6 replies to this topic

#1 simonsays

simonsays
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 06 August 2006 - 01:30 PM

The problem is the following. I make a new array from database results:

$allpaths[]=array ("id" => $row['id'], "path" => $path);

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:

foreach ($allpaths as $key => $row) {
   $id[$key]  = $row['id'];
   $path[$key] = $row['path'];
}

array_multisort($path, SORT_ASC, $id, SORT_ASC, $allpaths);

But this does not seem to work and all the time I get Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag error...

The complete function code is here...

<?
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;

}

?>

I would be very grateful if someone could really help me.

#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 06 August 2006 - 02:39 PM

what does your Path() function do? And what does it do with your $row['id']?

P.S.: remove the $allpaths from you array_multisort();

#3 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 02:45 PM

Doesn't look like you need array_multisort() for this, can you not just use usort()?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#4 simonsays

simonsays
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 06 August 2006 - 03:06 PM

what does your Path() function do? And what does it do with your $row['id']?

P.S.: remove the $allpaths from you array_multisort();


Path function forms a complete category path to some lower category (e.g. Monitors>CRT, Printers> Laser > Epson, etc.)

Here it is
function Path($child){
$cat=getParents($child);
$path=getCatName($child);
foreach ($cat as $element)
{$path = $element['n'].' > '.$path ;}
return $path;}

I removed $allsorts, but it didn't seem to help much.

Doesn't look like you need array_multisort() for this, can you not just use usort()?


array_multisort() seemed to be less complicated for me. do you think usort() is easier to use?

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 03:20 PM

Well from the page in the manual from this very site:

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.


so, although I've never used it, it would seem array_multisort() isn't quite going to do what you need.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 simonsays

simonsays
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 06 August 2006 - 03:53 PM

the code that I took for array_multisort comes directly from php.net

#7 freshwebs

freshwebs
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 August 2006 - 05:58 PM

You should use usort.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users