Beauford Posted July 14, 2010 Share Posted July 14, 2010 Is PHP really this insane? I want to sort an array that has just numbers in it. Then I want to implode the result. $test= natsort($array); $num = implode("",$test); if $array has 5 2 1 3 4 in it then $num should end up looking like this: 1 2 3 4 5. Nope: Warning: implode(): Invalid arguments passed in file.php on line 171 -- If I just implode $array it works fine, but not sorted which is what I want. Beyond me why it doesn't work. Looks perfectly logical to me, so what am I missing??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/ Share on other sites More sharing options...
AbraCadaver Posted July 14, 2010 Share Posted July 14, 2010 What does the manual sys natsort() returns? So what is assigned to $test? Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086066 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 If you read the documentation for natsort(), you will find that it does not return an array. It simply sorts the array you pass it as a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086067 Share on other sites More sharing options...
Beauford Posted July 14, 2010 Author Share Posted July 14, 2010 If you read the documentation for natsort(), you will find that it does not return an array. It simply sorts the array you pass it as a parameter. So the question is then, how do I do it? This to me would have been the proper way of doing things, then how how can someone manipulate the sorted data? Any help on other alternatives would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086076 Share on other sites More sharing options...
AbraCadaver Posted July 14, 2010 Share Posted July 14, 2010 Did you look at the manual? It sorts the array, it does not return a sorted version of the array: natsort($array); $num = implode("",$array); Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086078 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 So, even after two different people suggested reading the documentation for that function, you apparently didn't Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086083 Share on other sites More sharing options...
Beauford Posted July 14, 2010 Author Share Posted July 14, 2010 ROFL What is the purpose of me reading the manual, you already told me it doesn't work. Maybe you people should read my posts closer which specifically asked for alternatives to natsort so that I can acheive the desired results. Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086088 Share on other sites More sharing options...
AbraCadaver Posted July 14, 2010 Share Posted July 14, 2010 ROFL What is the purpose of me reading the manual, you already told me it doesn't work. Maybe you people should read my posts closer which specifically asked for alternatives to natsort so that I can acheive the desired results. Maybe you should read our posts more closely, because no one told you it doesn't work, we said you aren't using it properly, hence the manual suggestion. natsort() is perfect in this case. Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086094 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 WOW. Total lack of It simply sorts the array you pass it as a parameter. AND (with code even, just copy/paste) - It sorts the array, it does not return a sorted version of the array: natsort($array); $num = implode("",$array); Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086096 Share on other sites More sharing options...
premiso Posted July 14, 2010 Share Posted July 14, 2010 Just READ THE FUCKING MANUAL. Come on now! EDIT: Removed images for everyone's sake. Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086101 Share on other sites More sharing options...
Mchl Posted July 14, 2010 Share Posted July 14, 2010 What is the purpose of me reading the manual, [...] Can we elect this as a quote of the month? Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086103 Share on other sites More sharing options...
Beauford Posted July 14, 2010 Author Share Posted July 14, 2010 ROFL What is the purpose of me reading the manual, you already told me it doesn't work. Maybe you people should read my posts closer which specifically asked for alternatives to natsort so that I can acheive the desired results. Maybe you should read our posts more closely, because no one told you it doesn't work, we said you aren't using it properly, hence the manual suggestion. natsort() is perfect in this case. How is it perfect? Is reading the manual going to tell me this, no it is not. Ane lets get one thing straight, I read the manual - how do you think I even found out about it. Now maybe your seeing something I'm not, but before just blabbing about reading the manual, maybe you should explain why. Not eveyone reads something the same way. I find this more often than not in these forums where people just casually through out the manual crap. Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086107 Share on other sites More sharing options...
premiso Posted July 14, 2010 Share Posted July 14, 2010 Return Values Returns TRUE on success or FALSE on failure. The return values should really tell you what should be returned, if it returns a boolean, well chances are the array is passed by reference. We can easily find this out by looking under the "Description:" bool natsort ( array &$array ) The & represents that the array is passed by reference, which means that the variable which is passed in will contain the updated information. This is not to mention the Example usages section: <?php $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png"); asort($array1); echo "Standard sorting\n"; print_r($array1); natsort($array2); echo "\nNatural order sorting\n"; print_r($array2); ?> Which show you how to properly use the function. Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086114 Share on other sites More sharing options...
Beauford Posted July 14, 2010 Author Share Posted July 14, 2010 I appreciate the effort to explain this, but I still don't see how I can use this to solve my problem - if it actually can be. The examples are useless and doesn't show me how it applies to what I want to do. If all I can do with this is use print_r it makes no sense to me. Sorry if I making this harder than it is, but just not getting this. I need a sorted array that I can use just like the original unsorted one. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086153 Share on other sites More sharing options...
AbraCadaver Posted July 14, 2010 Share Posted July 14, 2010 I appreciate the effort to explain this, but I still don't see how I can use this to solve my problem - if it actually can be. The examples are useless and doesn't show me how it applies to what I want to do. If all I can do with this is use print_r it makes no sense to me. Sorry if I making this harder than it is, but just not getting this. I need a sorted array that I can use just like the original unsorted one. Thanks OK, I will attempt to explain and beyond that you'll have to be more specific about what you don't understand: $array = array(5, 2, 1, 3, 4); print_r($array); natsort($array); //$array has been sorted print_r($array); $num = implode("",$array); echo $num; Output: Before sort: Array ( [0] => 5 [1] => 2 [2] => 1 [3] => 3 [4] => 4 ) After sort: Array ( [2] => 1 [1] => 2 [3] => 3 [4] => 4 [0] => 5 ) After implode(): 12345 Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086160 Share on other sites More sharing options...
Beauford Posted July 15, 2010 Author Share Posted July 15, 2010 Got it. Thanks. No sure what exactly was confusing me, but for some reason I was thinking natsort($array); needed to be assigned to another array or something. I wasn't thinking I could just use it as shown below. natsort($array); $num = implode("",$array); I was almost there in my first post, but the manual just made it more confusing. $test = natsort($array); $num = implode("",$test); Quote Link to comment https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086170 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.