mr_dna Posted January 22, 2008 Share Posted January 22, 2008 Hi everyone, This is my first post and I am a noob to PHP ??? - I've only been at it for a few weeks, so I may be making an obvious mistake. I'm writing a script to replace Apache's built in file indexer (i.e. when you open a directory without an index.html, Apache will provide a list of files and allow the user to sort them by name, date, etc. ). My server is running PHP 4.3, so there isn't much in the way of OOP, so I'm using a multidimensional array to store the list of files and PHP's built-in array sorting routines to sort them. Trouble is, when I pass an array reference to one of my sorting functions I'm not getting any sorting back, the array output is unchanged. Anyway, here's a simplified example of what I'm trying to do: [pre]<?php function sort_name( &$data ) { foreach ( $data as $key => $value ) { $name[$key] = $value['name']; $score[$key] = $value['score']; } array_multisort( $name, $score, $data ); } function sort_score( &$data ) { foreach ( $data as $key => $value ) { $name[$key] = $value['name']; $score[$key] = $value['score']; } array_multisort( $score, $name, $data ); } $data = array( array( "index" => 0, "name" => "F", "score" => 3 ), array( "index" => 1, "name" => "D", "score" => 1 ), array( "index" => 2, "name" => "A", "score" => 4 ), array( "index" => 3, "name" => "H", "score" => 3 ), array( "index" => 4, "name" => "G", "score" => 3 ), array( "index" => 5, "name" => "B", "score" => 1 ), array( "index" => 6, "name" => "E", "score" => 4 ), array( "index" => 7, "name" => "C", "score" => 3 ), ); echo( "Sorting \$data by name:" ); sort_name( $data ); print_r( $data ); echo( "Sorting \$data by score:" ); sort_score( $data ); print_r( $data ); ?>[/pre] Any thoughts on what I'm doing wrong would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/87151-help-with-sorting-an-array-by-reference/ Share on other sites More sharing options...
teng84 Posted January 22, 2008 Share Posted January 22, 2008 i don't know how you can sort that because you have multidimensional array and obviously your array index are 1,2,3,4,5 etc.. so sorting that out will always give the same result see this sample here <a href="http://www.php.net/manual/en/function.ksort.php">ksort </a> <a href="http://www.php.net/manual/en/function.sort.php">sort</a> Quote Link to comment https://forums.phpfreaks.com/topic/87151-help-with-sorting-an-array-by-reference/#findComment-445773 Share on other sites More sharing options...
mr_dna Posted January 22, 2008 Author Share Posted January 22, 2008 The $index is really only there for reference, so that I can see the original order of the array after sorting - it could be renamed $original_index or something to clear up the confusion. If you rewrite the code without the functions sort_name() and sort_score(), and add them to the body of the code as maybe an if/then statement, you will see that the sorting routines work fine. They only fail to work from within the function Quote Link to comment https://forums.phpfreaks.com/topic/87151-help-with-sorting-an-array-by-reference/#findComment-445818 Share on other sites More sharing options...
mr_dna Posted January 22, 2008 Author Share Posted January 22, 2008 Here's an example of the code without using functions, if you run it you will see that the array_multisort() sorts the arrays by name and then by score. The second array_multisort() sorts it by score and then by name, the $index is just there so that I can quickly see what the original order was. <?php $data = array( array( "original_index" => 0, "name" => "F", "score" => 3 ), array( "original_index" => 1, "name" => "D", "score" => 1 ), array( "original_index" => 2, "name" => "A", "score" => 4 ), array( "original_index" => 3, "name" => "H", "score" => 3 ), array( "original_index" => 4, "name" => "G", "score" => 3 ), array( "original_index" => 5, "name" => "B", "score" => 1 ), array( "original_index" => 6, "name" => "E", "score" => 4 ), array( "original_index" => 7, "name" => "C", "score" => 3 ), ); foreach ( $data as $key => $value ) { $name[$key] = $value['name']; $score[$key] = $value['score']; } echo( "Sorting \$data by name:<br />\n" ); array_multisort( $name, $score, $data ); echo( "<pre>\n" ); print_r( $data ); echo( "</pre>\n" ); echo( "<br /><br />" ); echo( "Sorting \$data by score:<br />\n" ); array_multisort( $score, $name, $data ); echo( "<pre>\n" ); print_r( $data ); echo( "</pre>\n" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87151-help-with-sorting-an-array-by-reference/#findComment-445819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.