Jump to content

[SOLVED] newb problem with sort() and asort()


iansane

Recommended Posts

Hi, I am fairly new to php. I'm learning from "Beginning PHP5" (2004). I'm having trouble with the sort() and asort() functions on arrays. They don't seem to be working right.

 

Here's the code:

<?php
$my_unsorted_array = array("jim", "bob", "mary");
$my_sorted_array = sort($my_unsorted_array);

//according to the book this is all there is to it
//now trying to display the sorted array just displays a "1" and nothing else

print_r($my_sorted_array);
//displays a "1"
echo $my_sorted_array;
//displays a "1"
echo $my_sorted_array[1];
//displays nothing and no error message

 

I have the same problem with asort()

 

Is this the correct way to use sort() if I want to have both a sorted and unsorted array available to work with? BTW print_r() works fine with the first unsorted array.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/
Share on other sites

just call sort(), it doesn't return an array

 

$my_unsorted_array = array("jim", "bob", "mary");
sort ($my_unsorted_array);
echo '<pre>', $my_unsorted_array, '</pre>';  // now sorted

 

 

That just displays the word "Array".

 

I tried print_r() and it worked. It actually sorted the array. If I wanted to still have a copy of the unsorted array, (I don't know if there's a good reason), I'm guessing I have to make a copy with another name and then sort just one of them? Is there some kind of copy_array() function? I know in c++ I would use a for loop and increment through each element writing it to another array. Is that what needs to be done in php?

 

Thanks

$array1 = array(1,3,6,5,2,7,;
$array2 = $array1; // you can always create a twin duplicate array simply like this.

foreach($array1 as $val){
echo $val;
}

echo '<br />';

sort($array2);
foreach($array2 as $val2){
echo $val2;
}

 

Is that what you are looking for?

 

-NRG

 

P.S You cannot simply echo $myarray.. it doesn't work like that.. usually, the use a foreach loop is used as seen in my example.

Additionally, do not use asort on indexed arrays.. asort is meant to sort out associative arrays... while sort is for indexed arrays.

 

Additionally, do not use asort on indexed arrays.. asort is meant to sort out associative arrays... while sort is for indexed arrays.

 

asort() will still preserve the key even on an indexed array, which may be required in some instances.

 

example

$vals = array (100, 250, 200);
$text = array ('aaa', 'bbb', 'ccc');

// sort both arrays according to value

asort($vals);
foreach ($vals as $k => $v)
{
    echo "$v : {$text[$k]}<br/>";
}

   
-->
100 : aaa
200 : ccc
250 : bbb

 

Additionally, do not use asort on indexed arrays.. asort is meant to sort out associative arrays... while sort is for indexed arrays.

 

asort() will still preserve the key even on an indexed array, which may be required in some instances.

 

Good point!

@ iansane

 

in addition to setting up twin arrays as such:

$array1 = array(1,3,6,5,2,7,;
$array2 = $array1; // you can always create a twin duplicate array simply like this.

 

you could also do this:

$array1 = $array2 = array(1,3,6,5,2,7,8,11,0);

 

Cheers,

 

NRG

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.