views:

96

answers:

2

Hi, I'm a php newbie, and I'm creating a Facebook application for my flash game. In the main page of the application, I want to print the current user friends sorted by score.

I get first user friends using my application with this API function:

<?php $friends = $facebook->api_client->friends_getAppUsers();?>

$friends is an Array with all user friends ID's, every ID is a bigint.

after that I create another array to store in Friends ID's + Scores:

<?php
foreach( $friends as $friend )
{
$fscores["$friend"] = get_user_bestscore($friend);
}
?>

get_user_bestscore($friend); Function get the score from my DB.

I sort the array to display friends sorted by score:

<?php sort($fscores); ?>

In final step to dispaly the $fscores array to show friends names from ID, and Score I use:

<?php
foreach( $fscores as $fid => $score )
{
echo '<P>';
echo '<fb:profile-pic uid="'.$fid.'" linked="true" /><br>';
echo '<b># '.$counter++.'</b>';
echo '<b>- <fb:name uid="'.$fid.'" useyou="false"/></b><br>';
echo '<b>Score : '.$score.'</b>';
echo '</P>';
}
?>

the $score var display the score stored from DB to array correctly, but $fid (Facebook Friend ID) display ex: 0

I used print_r to know $scores array content i found : Array ( [0] => 5.87 )

and in the first $friends array I found: Array ( [0] => 100000625691889 )

What I want to get is: Array ( [100000625691889 ] => 5.87)

Any solution please,

Thanks in advance.

+6  A: 

sort function will remove your keys and auto index.

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

Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.

you can use asort() to sort the array and preserve keys.

kali
beat me too it, +1
RobertPitt
Why not just use http://www.php.net/asort on it's own...I guess I do not see the need for this custom function at least for this scenario.
Brad F Jacobs
true, i'm gonno edit it
kali
+5  A: 

I'm not 100% sure I understand your question. It looks like you have an array that looks like this:

100000625691889 => 5.87

and you're losing the key. If so, that's because you're using sort(), which doesn't preserve array indexes. If you want to preserve array keys, use asort() instead.

Note: I don't mean sort by key (unless that's what you want). Sorting by key is done by ksort(). Let me clarify this with some examples:

$arr = array(1234 => 5, 5678 => 3, 3456 => 11);
sort($arr);
print_r($arr);
$arr = array(1234 => 5, 5678 => 3, 3456 => 11);
asort($arr);
print_r($arr);
$arr = array(1234 => 5, 5678 => 3, 3456 => 11);
ksort($arr);
print_r($arr);

Sorting by value (sort()):

Array
(
    [0] => 3
    [1] => 5
    [2] => 11
)

Sorting by value preserving keys (asort()):

Array
(
    [5678] => 3
    [1234] => 5
    [3456] => 11
)

Sorting by key (ksort()):

Array
(
    [1234] => 5
    [3456] => 11
    [5678] => 3
)
cletus
Sorting Associatavly would be the simplest route :) +1
RobertPitt
Thanks for the help (y), worked great with asort, I have to read more and more about array functions and test.
McShark