Jump to content

Recommended Posts

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 ;D

Link to comment
https://forums.phpfreaks.com/topic/87151-help-with-sorting-an-array-by-reference/
Share on other sites

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>

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  :(

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" );

?>

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.