Jump to content

sorting associative array - trouble


simonsays

Recommended Posts

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
Share on other sites

[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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.