我想在Twitter上回应某个帐户关注者的位置列表。我目前得到的结果如下:
Array
(
[users] => Array
(
[0] => Array
(
[id] => 2247341036
[id_str] => 2247341036
[name] => Niall Horan
[screen_name] => srthoranpayne
[location] => london biben
[description] => My dreams come true
[url] =>
[entities] => Array
(
[description] => Array
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 325
[friends_count] => 641
[listed_count] => 0
[created_at] => Sun Dec 15 15:10:06 +0000 2013
[favourites_count] => 63
[utc_offset] =>
[time_zone] =>
[geo_enabled] =>
[verified] =>
[statuses_count] => 223
[lang] => es
[status] => Array
[etc etc]
但我想只有
[location] => london biben
在此之后我可以添加的代码可以帮助我吗?我认为它是循环“foreach”的东西,但我不知道如何把它。我该如何修改最后一行?
$url = "https://api.twitter.com/1.1/followers/list.json";
$requestMethod = "GET";
$getfield = '?screen_name='.$visitor.'&count=200';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3> <p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]." </em></p>";exit();}
echo "<pre>";
print_r($string);
echo $string[user]["location"];*/
foreach($string as $items)
{
echo "Tweeted by: ".$['user']['location']."<br />";
}
提前致谢。
编辑:我添加了我已经尝试过的最后一行代码但没有成功。
编辑下面:当前代码。
$url = "https://api.twitter.com/1.1/followers/list.json";
$requestMethod = "GET";
else {$user = "$visitor";};
$getfield = '?screen_name='.$visitor.'&count=200';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3> <p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]." </em></p>";exit();}
echo "<pre>";
print_r($string);
foreach($data as $items)
{
for($i = 0; $i < count($items); $i++) {
echo "Location: " . $items[$i]['location']. "<br />";
}
}
答案 0 :(得分:1)
你循环结果如下:
foreach($string as $items)
{
echo "Tweeted by: " . $items[0]['location'] . "<br />";
}
编辑:
这是一个更强大的例子:
$data = Array
(
"users" => Array
(
"0" => Array
(
"id" => "2247341036",
"id_str" => "2247341036",
"name" => "Niall Horan",
"screen_name" => "srthoranpayne",
"location" => "london biben",
"description" => "My dreams come true",
"url" => "",
"entities" => Array
(
"description" => Array
(
"urls" => Array
(
)
)
),
"protected" => "",
"followers_count" => "325",
"friends_count" => "641",
"listed_count" => "0",
"created_at" => "Sun Dec 15 15:10:06 +0000 2013",
"favourites_count" => "63",
"utc_offset" => "",
"time_zone" => "",
"geo_enabled" => "",
"verified" => "",
"statuses_count" => "223",
"lang" => "es",
"status" => Array ()
),
"1" => Array
(
"id" => "2247341036",
"id_str" => "2247341036",
"name" => "Niall Horan",
"screen_name" => "srthoranpayne",
"location" => "berlin germany",
"description" => "My dreams come true",
"url" => "",
"entities" => Array
(
"description" => Array
(
"urls" => Array
(
)
)
),
"protected" => "",
"followers_count" => "325",
"friends_count" => "641",
"listed_count" => "0",
"created_at" => "Sun Dec 15 15:10:06 +0000 2013",
"favourites_count" => "63",
"utc_offset" => "",
"time_zone" => "",
"geo_enabled" => "",
"verified" => "",
"statuses_count" => "223",
"lang" => "es",
"status" => Array ()
)
)
);
foreach($data as $items)
{
for($i = 0; $i < count($items); $i++) {
echo "Tweeted by: " . $items[$i]['location']. "<br />";
}
}
输出:
Location: london biben
Location: berlin germany
编辑:我可能不太清楚,但你必须混合使用foreach和for循环来获得你想要的结果:
foreach($data as $items)
{
for($i = 0; $i < count($items); $i++) {
echo "Location: " . $items[$i]['location']. "<br />";
}
}
现在,您将在数组中前进并获得所需的输出。 (: