基本上,我有数组$name
,$followers
和$imageurl
,我想以数字方式排列$followers
数组,但也会更新{$name
数组的排列相应地,1}}和$imageurl
。
例如,$followers[0] = 2783848
,$name[0] = "Rob"
和$imageurl[0] = "http://xxx.com/image.jpeg"
,其中所有三个数组都通过具有相同的数组键进行链接。
好的方法,这是我的整个代码:
$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;
}
任何建议/答案/评论将不胜感激:)!!
我也不确定这是查询Twitter API的最佳方式,所以如果有人有任何建议,请随时留在这里:)。
更新
我想我已经找到了一种更准确地说明这一点的方法;
我有数组$name
,$followers
和$imageurl
,其中每个值都是相对的,例如$name[0]
与$followers[0]
和$imageurl[0]
相关。我想创建第四个数组$rank
,其中包含根据关注者的相对值排序的值1-15
,例如:
$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;
这应该可以更容易地弄清楚我在寻找什么。
答案 0 :(得分:0)
保留钥匙无关紧要。为此使用asort
。循环遍历名称数组时,值将按字母顺序排列(或者,取决于您的asort参数),索引(键,数字)将不在数字顺序。
//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>';
}
更新
//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;
}
然后你可以遍历排名,显示它,或插入语句或其他任何内容。
foreach ($ranks as $rank=>$key) {
echo "{$name[$key]} has rank $rank";
$sql="INSERT INTO ranks ('username','followers','rank') VALUES ('{$name['$key']}','{$followers['$key']}','$rank')";
}