nezbie Posted December 17, 2006 Share Posted December 17, 2006 Hi, this consideres my csv manager. It reads a csv file and drops the info to a table and to inputfields so they are editable. The basic structure goes like the following (example with 2 rows and 3 columns:[b]| index | First name | Last name | ID |[/b]| 1 | John | Smith | 2920 || 2 | Harry | Shearer | 1032 |in the very first line of my csv file the column headers are defined. (in this case first name, last name, id)I'd need to be able to sort these information by column index (name is also possible if that is what is needed)I mentioned earlier that those information are found in inputfields. They are named like: [b]contents[rowNR][columnNR][/b], so "Shearer" is found in [b]$contents[2][2][/b]After I edit this info it is send via form to be rewritten to it's original sourcefile using the following simple piece of code:[code]foreach ($_POST['contents'] as $key=>$value) { $parsedData .= implode(';', $value) . "\r\n";}[/code]So what I'd need help with is sorting the array $contents[row][[b]with this key[/b]]. Also I'd need to be able to s[b]kip the row with index "0"[/b] because it is used to save the column header information. I hope you understand what I'm after here..All help and ideas highly appreciated, thanks =) Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/ Share on other sites More sharing options...
doni49 Posted December 18, 2006 Share Posted December 18, 2006 Not sure if it's the cleanest way, but it should get the job done:[code]<?phpfunction doSort($arr, $key){ $tmp[] = $arr[0]; $arr = array_slice($arr, 1); foreach ($arr as $a){ $order[] = $a[0]; } $order = asort($order, TRUE); foreach($order as $idx =>value){ $tmp[]=$arr[$idx]; } return $tmp;}$contents = doSort($contents, "index");?>[/code] Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-143242 Share on other sites More sharing options...
Barand Posted December 18, 2006 Share Posted December 18, 2006 Use uasort() with a custom sort function Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-143245 Share on other sites More sharing options...
Psycho Posted December 18, 2006 Share Posted December 18, 2006 Or, you could create the array in columns so that you can use array_multisort which youcan use to sort by columnA, then by column B, etc., or however you want it.See the manual here: http://us3.php.net/manual/en/function.array-multisort.phpIn any event it would be better design to not include the headers as a data item. Instead use the header items as the keys in the array. Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-143410 Share on other sites More sharing options...
nezbie Posted December 20, 2006 Author Share Posted December 20, 2006 I don't get it.. Really.So if I want to sort array $foo[x][y] ascending by "y", I can't find any clear answers to that, if it's even possible with as little code as possible? Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-145076 Share on other sites More sharing options...
Psycho Posted December 20, 2006 Share Posted December 20, 2006 The reason it is more difficult than it needs to be is that you have included the header names as a record. The header records should be the keys of the array. If you were to put the data in columns instead of row you could sort it using just a single line with the array_Multisort() function.If you don't want to reformat your array then you need to look at uasort() as Barand suggested. Have you looked at the manual yet?: http://us3.php.net/manual/en/function.uasort.php Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-145188 Share on other sites More sharing options...
nezbie Posted December 20, 2006 Author Share Posted December 20, 2006 Yes I have. It's just as clear as usual ;)I didn't quite understand the keys and headers thing.. should it be something like columnName=>[rowNumber] = value ? I've never been too good with these anyways - and if there is no at least moderately easy solution with current code logic I will just leave this feature not to be included.. It's not so important, rather "nice to have".I don't want to rewrite my script for this. Would I have to? Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-145406 Share on other sites More sharing options...
Barand Posted December 20, 2006 Share Posted December 20, 2006 This will do it using a cutom sort function[code]<?php$data = array ( array ('index','fname','lname','id'), array (1, 'John', 'Smith', 2920), array (2, 'Harry', 'Shearer', 1032), array (3, 'John', 'Anderson', 5970), array (4, 'Harry', 'Taylor', 1256),);function mysort($a , $b) { /** * custom sort function takes pairs of elements, $a and $b * If a should sort before b, return -1 * If a should be after b, return 1 * If they are equal, return 0 */ global $sortByCol; if ($a[0]=='index') return -1; if ($b[0]=='index') return 1; return strcmp($a[$sortByCol], $b[$sortByCol]); }$sortByCol = 2; // set to last name coluasort ($data, 'mysort');// checkecho '<pre>', print_r($data, true), '</pre>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-145468 Share on other sites More sharing options...
nezbie Posted December 21, 2006 Author Share Posted December 21, 2006 Ok, that works, thank you very much :) Link to comment https://forums.phpfreaks.com/topic/31025-solved-sorting-a-dynamic-array/#findComment-145736 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.