Array ksort only shows non-like values?
Asked Answered
K

2

1

Have an array for a ranking script. Some times the key will be the same. They are numeric. When the sort is ran, only non-like values are echoed. Can't figure out the fix.

$list = array( $value1 => 'text', $value2 => 'text', $value3 => 'text');

krsort($list);
foreach ($list as $key => $frame) {
    echo $frame;
}
Kellykellyann answered 1/6, 2013 at 17:10 Comment(5)
you mean if $value1 == 1 and $value3 == 1, only one entry is shown?Secundine
What is the expected behavior and current behavior?Monika
Correct. Same if all 3 have the same numeric value, then only the first-ish entry in the array is shown.Kellykellyann
Expected -> Sort by numeric. If some values are same, print in array order. Current -> Sort by numeric. If some values are same, prints only first one in array.Kellykellyann
It is simply not possible to have multiple items in the array using the same key. In your example above, if you print $list, you'll see that it would have already substituted duplicate keys with the last value assigned. Please fix your code so that the keys are unique.Smoothbore
I
0

Going by what you wrote in the comments to this question and my other answer, I'd recommend to switch keys and values.

<?php 

$list = array( "frame1" => 4, "frame2" => 2, "frame3" => 99, "frame4" => 42 );

arsort($list);
foreach ($list as $frame => $ranking) {
    echo $frame;
}

?>
Inductee answered 1/6, 2013 at 18:39 Comment(2)
I don't know why this works and the other way doesn't but sir Hauke P., a slow drawn out clap for you. Seems to show all values regardless of same numeric. Thanks! So simple, so confused. Oh well...moving on.Kellykellyann
You're welcome. And concerning your question one comment earlier: As long as your frame names are unique, yes.Inductee
I
2

If you assign two values to the same key in an array, the first value will be overridden by the second. You'll therefore end up with only one value for that key in the array.

To resolve this, I'd suggest to change your array structure like this:

<?php 

$list = array( $key1 => array($key1member1, $key2member2),
               $key2 => array($key2member1),
               $key3 => array($key3member1, $key3member2, $key3member3) );

krsort($list);
foreach ($list as $key => $frames) {
    foreach ($frames => $frame) {
        echo $frame;
    }
}

?>
Inductee answered 1/6, 2013 at 17:19 Comment(5)
My currently planned array is 50 values and changes which makes this unlikely. Is there another method of sorting where like keys won't be overridden?Kellykellyann
It heavily depends on how you create and access the $list variable. If you add more code about its assignment and accesses to your question, you might get other answers.Inductee
You just Don King'd me because I'm not even sure what you are implying. I'm thinking I might have to compare keys before hand and have them adjust to not be like...which then again, seems robust.Kellykellyann
My questions boil down to: How do you create the $list array and what code is responsible for that? Where and how do you use the array later? Is the sorting method and the foreach loop really the only place where you use it, i.e. access its members?Inductee
Single use and the value echoed is written by hand. A name has a ranking on a page up/down with a single total printed to a text file. The array loads the list of names and when the page loads, sorts by the ranking from the text file(s).Kellykellyann
I
0

Going by what you wrote in the comments to this question and my other answer, I'd recommend to switch keys and values.

<?php 

$list = array( "frame1" => 4, "frame2" => 2, "frame3" => 99, "frame4" => 42 );

arsort($list);
foreach ($list as $frame => $ranking) {
    echo $frame;
}

?>
Inductee answered 1/6, 2013 at 18:39 Comment(2)
I don't know why this works and the other way doesn't but sir Hauke P., a slow drawn out clap for you. Seems to show all values regardless of same numeric. Thanks! So simple, so confused. Oh well...moving on.Kellykellyann
You're welcome. And concerning your question one comment earlier: As long as your frame names are unique, yes.Inductee

© 2022 - 2024 — McMap. All rights reserved.