开发者

Creating array containing ranks based on the values of another array?

Basically, I have the arrays $name, $followers and $imageurl, and I would like to arrange the $followers array numerically, but also update the arrangement of $name and $imageurl accordingly.

For example, $followers[0] = 2783848, $name[0] = "Rob" and $imageurl[0] = "http://xxx.com/image.jpeg" where all three arrays are linked through having the same array key.

For good measure, here's my whole code:

$screennames = array(
                 0 => "LilKim",
                 1 => "NickiMinaj",
                 2 => "drakkardnoir",
                 3 => "LilTunechi",
                 4 => "kanyewest",
                 5 => "RealWizKhalifa",
                 6 => "beyonce",
                 7 => "KELLYROWLAND",
                 8 => "LupeFiasco",
                 9 => "TinieTempah",
                 10 => "50cent",
                 11 => "TRINArockstarr",
                 12 => "iamdiddy",
                 13 => "Timbaland",
                 14 => "chrisbrown"
                     );

for($i = 0; $i < 15; $i++) {
$xml[$i] = @simplexml_load_file('http://api.twitter.com/1/users/show.xml?screen_name=' . $screennames[$i]);
$name[$i] = $xml[$i]->name;
$followers[$i] = $xml[$i]->followers_count;
$imageurl[$i] = $xml[$i]->profile_image_url;
}

Any advice/answers/comments will be greatly appreciated :)!!

I'm also not sure that this is the best way to query the Twitter API, so if anyone has any recommendations, feel free to leave them here :).

UPDATE:

I think I've found a way to 开发者_运维问答put this more precisely;

I have the arrays $name, $followers and $imageurl where each value is relative, e.g. $name[0] is relative to $followers[0] and $imageurl[0]. I would like to create a fourth array $rank which contains values 1-15 ordered based on the relative value of followers, for example:

$followers = array(0 => 278738, 1 => 32784, 2 => 103562, 3 => 37848); // Etc from keys 0 - 14

$rank[0] = 1;
$rank[1] = 4;
$rank[2] = 2;
$rank[3] = 3;

That should make a lot easier to figure out what I'm looking for.


It shouldn't matter if you preserve your keys. For that use asort. When you loop through the name array, the values will be arranged alphabetically(or however, depending on your asort params) and the indexes (the keys, the numbers) will be out of numeric order.

//show by name alphabetically
asort($names); //puts names in alphabetic order, while preserving key associations
foreach ($names as $key=>$value) {
    echo "$value has " . $followers[$key] . ' followers<br>';
}

//show ordered by most followers
arsort($names, SORT_NUMERIC); //puts followers in reverse numeric order, while preserving key associations
foreach ($followers as $key=>$value) {
    echo $name[$key] . ' has $value followers<br>';
}  

Update

//Create rank array
$rank=0;
arsort($names, SORT_NUMERIC); //puts followers in reverse numeric order, while preserving key associations
foreach ($followers as $key=>$value) {
  $rank++; 
  $ranks[$rank]=$key;
}  

Then you can loop through ranks, and display it, or do insert statements or whatever.

foreach ($ranks as $rank=>$key) {
   echo "{$name[$key]} has rank $rank";
   $sql="INSERT INTO ranks ('username','followers','rank') VALUES ('{$name['$key']}','{$followers['$key']}','$rank')";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜