Functions 1 Ex 8 - Sort question


#1

Here is my code that is ok - but issue is it only works if I ignore the sort altogether and go back to using unsorted array.

$closest=array();

array_push($closest, "PM");
array_push($closest, "FF");
array_push($closest, "LM"); 
array_push($closest, "AL");
// Sort the list

$sorted=sort($closest);

$number =count($closest)-1;

$myranwin = rand(0,$number);
print strtoupper($closest[$myranwin]);
?>
Output LM or whatever.

I looked into it by printing each step to screen. If I print out $sorted i.e. print join(",", $sorted);- I get
join(): Invalid arguments passed (line 13) AL
that is just the first member of the array - and this one only member of the array persists through the rest of the program, I think the invalid argument comes up because $sorted = sort($closest) is not an array, therefore I can't work with it.
I know I could avoid the question by just using sort = ... though I don't understand why its in the exercise if it makes no difference.

What I am wondering is does this mean I can only save a manipulated array by explicitly creating a separate array, $sort=array()? Or have I missed something? And what is the program actually doing when I write $sorted = sort($closest);? Is it just saving the first element it encounters as a variable?


#2

In case that was not clear I meant it does not work if I follow through using $sorted instead of using $closest in $number and print strrtoupper lines as in
$number =count($sorted)-1;
print strtoupper($sorted[$myranwin]);


#3

try this

<?php
$closest=array();

array_push($closest, "PM");
array_push($closest, "FF");
array_push($closest, "LM"); 
array_push($closest, "AL");

// Sort the list
//removed the variable declaration here
sort($closest);
//test it here print it out to see if it is sorted
print_r($closest);
print "<br><br>";


$number =count($closest)-1;

$myranwin = rand(0,$number);	
print strtoupper($closest[$myranwin]);
?>

#4

Thanks Rydan. That gives me a print out of the sorted results, which I can then work with, but my question (which was not expressed clearly) was more:
Can I apply a command ,like sort, to an array and have the result as a new array without explicitly creating a new array and pushing the items in the new format? That is, my $sorted = sort($closest); - and then use $sorted instead of $closest from that point on (because I probably didn't sort it for no reason).
I suspect I would have to create the new array $sorted = array(); but then there must be a simple way to save the transformed (sorted) $closest array into $sorted so I can just use that.


#5

@bytemaster07734
as far i have seen you cannot do it explicitly like this

$result = sort($closest);

the above code if echoed will not return an array but will return 1 if you test it in your code

You would have to copy(shallow copy) the array to another variable like this

$my_array_copy = $closest;

//then now you can do

sort($my_array_copy);

Hence you can now use my_array_copy.

Note:: you do not have to individually push all elements to the new array


#6

well this is the code I made for selecting the winner name randomly.. :grin:
tried my best to make the code small :sunglasses: hope you all like it .:sweat_smile:


#7

what's wrong with this
<?php
$family_=array();

array_push($family_, "mum");
array_push($family_, "dad");
array_push($family_, "harry");
array_push($family_, "zac");
array_push($family_, "nic");
array_push($family_, "charlotte");
// Sort the list
//removed the variable declaration here
sort($family_);
//test it here print it out to see if it is sorted
print_r($family_);
print "

";

$number =count($family_)-1;

$myranwin = rand(0,$number);
print strtoupper($family_[$myranwin]);
?>