PDA

View Full Version : sort array by last item's value


leonthelion
12-15-2008, 04:03 AM
Hi,

I have to use a flat file database for a current project and am in need of a simple sort method for sorting by the last item/column in the array.

I'm quite a newb with arrays and I may be handling this the wrong way, but would prefer a quick and easy fix for prototyping :)

my db structure:

field1|field2|field3 ...
field1|field2|field3 ...
field1|field2|field3 ...

each line is read into an array from text file. when displaying output on html page, am exploding using pipe symbol.

before displaying, I would like to sort the "rows" in the array to the last field's numeric value.

here is my php for reference:


<?php
$fp = fopen('test.db','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
$ac = 0;
while (!feof($fp)) {
$lines[$ac] = fgets($fp,3062); //use 2048 if very long lines
$ac++;
$fp++;
}

fclose($fp);

//sort by something



echo '<table width="8000" border="1">
<tr>
<th>id</th>
<th>region</th>
<th>title</th>
<th>visible</th>
<th>age</th>
<th>materials</th>
<th>cost</th>
<th>areas</th>
<th>space</th>
<th>level</th>
<th>period</th>
<th>producer</th>
<th>img1</th>
<th>caption1</th>
<th>img2</th>
<th>caption2</th>
<th>img3</th>
<th>caption3</th>
<th>img4</th>
<th>caption4</th>
<th>img5</th>
<th>caption5</th>
<th>body1</th>
<th>body2</th>
<th>sort_order</th>
</tr>
';
for($x=0;$x<count($lines);$x++){
$a= explode("|",$lines[$x]);
echo '<tr>
<td>'.$a[0].'</td>
<td>'.$a[1].'</td>
<td>'.$a[2].'</td>
<td>'.$a[3].'</td>
<td>'.$a[4].'</td>
<td>'.$a[5].'</td>
<td>'.$a[6].'</td>
<td>'.$a[7].'</td>
<td>'.$a[8].'</td>
<td>'.$a[9].'</td>
<td>'.$a[10].'</td>
<td>'.$a[11].'</td>
<td>'.$a[12].'</td>
<td>'.$a[13].'</td>
<td>'.$a[14].'</td>
<td>'.$a[15].'</td>
<td>'.$a[16].'</td>
<td>'.$a[17].'</td>
<td>'.$a[18].'</td>
<td>'.$a[19].'</td>
<td>'.$a[20].'</td>
<td>'.$a[21].'</td>
<td>'.$a[22].'</td>
<td>'.$a[23].'</td>
<td>'.$a[24].'</td>
</tr>';
}
echo '</table';
?>

tBeck
12-20-2008, 05:15 PM
Store all your arrays into one array creating a multidemensional array. This will help make it easier to sort out all the data. Heres a modified function I created that you can use:


// required parameters: $array, $index; $index being where you want to start your search
// in the nested arrays. You can specify what sorting method you want either ascending
// or descending (asc or desc). Whether you want natural sorting, which is how humans
// would do it; and finally whether it's case sensitive.
function sortmd($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE){
if(is_array($array) && count($array)>0){
foreach(array_keys($array) as $key)
$temp[$key]=$array[$key][$index];
if(!$natsort)
($order=='asc')? asort($temp) : arsort($temp);
else{
($case_sensitive)? natsort($temp) : natcasesort($temp);
if($order!='asc')
$temp=array_reverse($temp,TRUE);
}
foreach(array_keys($temp) as $key)
(is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
return $sorted;
}
return $array;
}


Create your master array and give it this, change the index to wherever you want.


print_r(sortmd($array,2,'asc',true));