Solved

usort() function producing bad results on a linux environment, ok on windows

Posted on 2009-07-08
4
276 Views
Last Modified: 2012-05-07
This is really weird, the same code works on windows, but not on linux.

When I run the code below on a windows machine with PHP 5.2.5, it works perfect, I get the output array alphabetically ordered.

When I run it on a linux box running PHP 5.1.6 I get an array that doesn't have the same order as the original array, but it is also not properly sorted.

Do you guys know what part of my code is "environment dependant"? I can hardly believe the same code will produce two different results in different platforms...


// The code here:
 
class TestTwo{
 
                      // This is just the contructor / driver:
	function TestTwo(){
 
                                          // bootstrapping the input array:
		$sort_array = file_get_contents('test_array_sort.txt');
		$attributes_array = unserialize($sort_array);
 
		$this->attributes_array_sorted = $this->createAttributesArraySorted($attributes_array);			
                                          // At this point, I get the right output on Windows, and the badly sorted array in Linux
	}
	
	private function createAttributesArraySorted($attributes_array_){
		
		foreach($attributes_array_ as &$val){
 
			usort($val['children_nodes'], 'TestTwo::cpm_node');
 
		}
		return $attributes_array_;
		
	} /* end private function createAttributesArraySorted($attributes_array_) */
	
    /*
     * Auxiliary function used to sort attributes in alphabetical order 
     */
    static function cpm_node($a,$b){
    	
    	return strcasecmp($a['node_name'], $b['node_name']);
 
    } /* end static function cpm_nodes() */
    
} /* end class TestTwo */

Open in new window

0
Comment
Question by:cabrera48
  • 2
  • 2
4 Comments
 

Author Comment

by:cabrera48
ID: 24809757
A bit more of information: it looks like the cpm_node() function never gets called on linux.

I put a print statement inside of it, and I can see the print output on windows, on linux it doesn't display.

So the problem seem to be in this line:

usort($val['children_nodes'], 'TestTwo::cpm_node');
 
Why would that fail to call TestTwo::cpm_node static function on linux?
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 24814811
See the comments on static method calls/versions here:
http://us3.php.net/manual/en/language.pseudo-types.php#language.types.callback
appears that you are using a version prior to 5.2.3:
array('TestTwo', 'cpm_node')

Although I didn't test the above.
0
 

Author Comment

by:cabrera48
ID: 24815390
You are right, that was the problem, nothing to do with linux, but the PHP version I was using there.

Thank you.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24816396
Thanks for the question and the points.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question