Jump to content

[SOLVED] how to sort multi-dimensional array by value


SchweppesAle

Recommended Posts

hi, I'd like to sort a multidimensional array by the values of each element.  I'm having some trouble though.

 

$list = $_POST["list"];
$count = count($list);
$max = max($list);

include("Connect.php");
DBConnect();
mysql_select_db("account6_jo152");

$result = mysql_query("Select * FROM jos_content ORDER BY created DESC") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id[] = $row['id'];
}

$maxID = max($id);
array_multisort($list, SORT_NUMERIC);
for ($i = 0; $i <= $count; $i++)
{
for ($t = 0; $t <= $maxID; $t++)
{
	if(($list[$i][$t]) != 0)
	{
		$itemList[] = $t;
	}
}
}

 

The idea is to sort the array out then remove all elements who's value == 0 before placing them into a seperate array.  Thanks in advance for your help.

Before/After using multisort() the output displayed by the action page seems to sort the array by ID rather than the value associated with all elements of the array.  Here's the code I used in generating it.

 

echo Generate_List();

function Generate_List()
{
$result = mysql_query("Select * FROM jos_content ORDER BY created DESC") or die(mysql_error());
$output = '<div style = "width:750px; margin-left:auto; margin-right:auto"><table border = "1" width = "750">';
$output .='<tr><td>Title</td><td>Created</td><td>Order</td></tr><form action = "generate_newsletter.php" method = "post">';
$counter = 1;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];	

$output .= '<tr><td>';
$output .= $row['title'];
$output .= '</td><td>'.$row['created'];
$output .= '</td><td><input type = "text" maxlength = "3" size = "5" name = "list['.$counter.']['.$id.']" value = "0" />';
$output .= '</td></tr>';	
$counter++;
}

$output .= '<input type = "submit" />';	
$output .= '</form></td></tr></table></div>';
return $output;
}

 

since it's in DESC order the newest content items are displayed at the top; usually these entries have the highest ID attribute.  The array itself should consist of about 127(position grows to 127) entries, most of them 0. 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.