PDA

View Full Version : sort() does not work???


Shining Arcanine
Thu 24th Jul '03, 10:11pm
In a nonvB script I am coding, sort() fails to operate. So does asort which is very similar to it. I am unable to reproduce my this issue in any script other than the one I am having it in.

I had 2 loops print the data that in the array before and after using sort and it stayed the same. Then I simulated what the area the problem was did and then printed the data with the same loops in a test script and it printed what I expected it to print.

http://us3.php.net/manual/en/function.sort.php

At the bottom "x-itec at freenet dot de" says:

Maybe u have a problem that you can not sort your array if the array contains only numbers. The solution: The numbers are sorted as ASCII, you have to store values to your array in this form as an example:
$artikelarray[$x]=intval($row188[id]);
You have to use INTVAL to convert your Number to a numeric value! Now you can sort your array with sort($artikelarray) and it works finde.
Boris Köster

I didn't think his workaround would work and lo and behold, it didn't. How do I get around this?

Edit: Here is the mutidimesional array that is generated on the fly which I am testing with:

$list=array(array(1,0),
array(10,1),
array(20,2),
array(30,3),
array(40,5),
array(50,4),
);

In another script, this will work with sort() but not in the script that I need it to work in. An extremely complex process is done to generate it so I can't simply put it in another script since this is part of an extremely complex script.

Scott MacVicar
Thu 24th Jul '03, 10:20pm
before the sort add

var_dump($arrayname);

and check the datatype of the value. Are you wanting to sort by key or value?

Shining Arcanine
Thu 24th Jul '03, 10:48pm
before the sort add

var_dump($arrayname);

and check the datatype of the value. Are you wanting to sort by key or value?

Sorry, I forgot to add that I am trying to sort a mutidimensional array by the first key in the subarray. This is actually part of a more advanced sort function I am trying to code. Here are the details:

$somearray(array('somevalue',3,2,4),
array('somevalue',1,3,2),
array('somevalue',0,2,4),
);

It would be great if you could tell me how to sort $somearray by $somearray[$anynum][1] instead of $somearray[$anynum][0] by default and have it subsitute $somearray[2][2] for $somearray[2][1] in the case of $somearray[2][1]; if $somearray[2][2] is null/0 it would substitute $somearray[2][3] for $somearray[2][1] instead of $somearray[2][2] and so on.

That is what I am pretty much trying to accomplish. My function is:

function cas (&$array, $value){
//CAS= Custom Array Sort
for ($x=0; $array[$x]; $x++){
$list[$x][1]=$x;
if ($array[$x][$value]){
$list[$x][0]=$array[$x][$value];
}else{
for ($y=0; !$array[$x][$value+$y]; $y++){
if ($array[$x][$value+$y+1]){
$list[$x][0]=$array[$x][$value+$y+1];
}
}
}
}

sort($list);

for ($x=0; $list[$x]; $x++){
$array2[$x]=$array[$list[$x][1]];
}
$array=$array2;
}

So either I figure out how to get sort() to work or I find another way to accomplish what I am trying to accomplish. Otherwise my script will not work. ^_^;;

Edit: Here is the data I am inputing into the function:

$movelist=array(
array(1, 1, 1, 1, 1, 1,),
array(144, 10, 10, 10, 10, 10,),
array(5, 20, 20, 20, 20, 20,),
array(118, 30, 30, 30, 30, 30,),
array(246, 0, 0, 50, 50, 50,),
array(94, 40, 40, 40, 40, 40,),
};
//The data array is generated on the fly, I had php output this so I would have an example of the code that is put through it.


cas ($movelist, 1);

Lats
Fri 25th Jul '03, 12:12am
Have a look at array_multisort

http://www.php.net/array_multisort

It may be what you're looking for.

Shining Arcanine
Fri 25th Jul '03, 12:45am
Have a look at array_multisort

http://www.php.net/array_multisort

It may be what you're looking for.

I have already taken array_multisort into consideration. The only problem is that it does not organize an array by specific values (determined by whether they are true or false) within the subarrays within it.