Array_multisort help please [message #170282] |
Tue, 26 October 2010 15:45 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
Member |
|
|
I'm having a problem with using the array_multisort() function.
One key is a string consisting entirely of numbers and the function
reads that as an index and reindexes the array, thus destroying the
actual values.
What I'm trying to achieve is to make sure that the second dimension is
always consistently sorted alphabetically. Eg. The key "siccles" should
always precede the key "UpperKEES' when both are present.
Thanks,
MikeB
This is what I have:
<?php
$array1 = array(
"10453"=> array(
"UpperKEES"=> array(
"ts"=>"1,2,3",
"score"=>"9854")
)
);
$array1["10453"]["siccles"] = array("ts"=>"4,5,6","score"=>"9854");
$array1["20095"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["32095"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["32095"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["45695"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["45695"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["53216"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["53216"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["66595"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["75489"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["89654"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["89654"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["99095"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["99095"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
$array1["99995"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
array_multisort($array1,SORT_REGULAR);
echo "<h2>Array 1 sorted var_dump</h2><br /><pre>";
var_dump($array1);
echo "</pre>";
?>
|
|
|
Re: Array_multisort help please [message #170284 is a reply to message #170282] |
Tue, 26 October 2010 17:33 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 26/10/10 16:45, MikeB wrote:
> I'm having a problem with using the array_multisort() function.
>
> One key is a string consisting entirely of numbers and the function
> reads that as an index and reindexes the array, thus destroying the
> actual values.
>
> What I'm trying to achieve is to make sure that the second dimension is
> always consistently sorted alphabetically. Eg. The key "siccles" should
> always precede the key "UpperKEES' when both are present.
>
> Thanks,
> MikeB
>
> This is what I have:
>
> <?php
> $array1 = array(
> "10453"=> array(
> "UpperKEES"=> array(
> "ts"=>"1,2,3",
> "score"=>"9854")
> )
> );
> $array1["10453"]["siccles"] = array("ts"=>"4,5,6","score"=>"9854");
> $array1["20095"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["32095"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["32095"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["45695"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["45695"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["53216"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["53216"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["66595"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["75489"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["89654"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["89654"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["99095"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["99095"]["UpperKEES"] = array("ts"=>"3,5,4","score"=>"4751");
> $array1["99995"]["siccles"] = array("ts"=>"3,5,4","score"=>"4751");
>
> array_multisort($array1,SORT_REGULAR);
>
> echo "<h2>Array 1 sorted var_dump</h2><br /><pre>";
> var_dump($array1);
> echo "</pre>";
> ?>
If you replace the line:
array_multisort($array1,SORT_REGULAR);
with the following:
// starts here
ksort($array1);
foreach ($array1 as $key => $value) {
ksort($value);
$array1[$key] = $value;
}
// ends here
Do you get the result you want?
Rgds
Denis McMahon
|
|
|
Re: Array_multisort help please [message #170285 is a reply to message #170284] |
Tue, 26 October 2010 18:20 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
Member |
|
|
Denis McMahon wrote:
>
> If you replace the line:
>
> array_multisort($array1,SORT_REGULAR);
>
> with the following:
>
> // starts here
> ksort($array1);
> foreach ($array1 as $key => $value) {
> ksort($value);
> $array1[$key] = $value;
> }
> // ends here
>
> Do you get the result you want?
>
Dennis, thank you, it does indeed.
I had to spend quite some time to try and figure out what your code was
doing, so if you don't mind, can you please check my comments and tell
me if I figured it out correctly?
> ksort($array1);
Sort the outer (high-level index of the array
> foreach ($array1 as $key => $value) {
iterate through each of the high-level indexes of the array, with
"$value" being a new array containing the sub-array that was in that key.
> ksort($value);
sort the sub-array
> $array1[$key] = $value;
replace the previous sub-array in $array1 with the new, sorted sub-array.
> }
So
essentially there isn't an in-place sort that would have done what I
wanted to do and this is a slick method to simulate that?
MikeB
|
|
|
Re: Array_multisort help please [message #170286 is a reply to message #170285] |
Tue, 26 October 2010 21:22 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 26/10/10 19:20, MikeB wrote:
> I had to spend quite some time to try and figure out what your code was
> doing, so if you don't mind, can you please check my comments and tell
> me if I figured it out correctly?
Looks about right. Oh, and I don't think it matters whether you sort the
second level or the top level first, so you could probably do the
ksort($array1) at the end if you wanted to.
> So
> essentially there isn't an in-place sort that would have done what I
> wanted to do and this is a slick method to simulate that?
I wouldn't call it slick, but it is a method, yes. It's specific to the
format of your data (although it will probably work for any 2d or higher
order array that you want to sort on the first two keys).
Rgds
Denis McMahon
|
|
|