Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086076
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086094
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086107
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086114
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086153
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086160
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/207756-sort-natsort-etc/#findComment-1086170
Share on other sites

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.