iansane Posted August 17, 2008 Share Posted August 17, 2008 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 More sharing options...
Barand Posted August 17, 2008 Share Posted August 17, 2008 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 Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-618526 Share on other sites More sharing options...
iansane Posted August 17, 2008 Author Share Posted August 17, 2008 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 Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-618535 Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 $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. Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-618576 Share on other sites More sharing options...
Barand Posted August 17, 2008 Share Posted August 17, 2008 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 Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-618583 Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 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! Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-618590 Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 @ 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 Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-618593 Share on other sites More sharing options...
iansane Posted August 18, 2008 Author Share Posted August 18, 2008 Thanks you guys. All the examples and explainations helped me understand arrays and php much better. I tried all the different ways and they all worked. Was able to move on in the book. Thanks for the help. I'll mark solved Link to comment https://forums.phpfreaks.com/topic/120068-solved-newb-problem-with-sort-and-asort/#findComment-619484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.