I want to output the value of followers_count just once. I managed to this using a foreach loop, however this obviously outputted the result for multiple times. Is there some other simple way I can do this?
the array is declared as follows:
$tweetsshow = $connection->get("https://api.twitter.com/1.1/users/show.json?screen_name=".$twitteruser);
echo $tweetsshow[0]->user->followers_count;
array(30) {
[0]=>
object(stdClass)#5 (24) {
["created_at"]=>
string(30) "Wed May 20 12:32:30 +0000 2015"
["id"]=>
float(6.010025574446E+17)
["id_str"]=>
string(18) "601002557444595712"
["text"]=>
string(79) "Judge Farrugia Sacco loses appeal http://t.co/njcgvTDTej http://t.co/GRjnBt13uC"
["source"]=>
string(71) "Times of Malta"
["truncated"]=>
bool(false)
["in_reply_to_status_id"]=>
NULL
["in_reply_to_status_id_str"]=>
NULL
["in_reply_to_user_id"]=>
NULL
["in_reply_to_user_id_str"]=>
NULL
["in_reply_to_screen_name"]=>
NULL
["user"]=>
object(stdClass)#6 (39) {
["id"]=>
int(390687940)
["id_str"]=>
string(9) "390687940"
["name"]=>
string(14) "Times of Malta"
["screen_name"]=>
string(15) "TheTimesofMalta"
["location"]=>
string(5) "Malta"
["description"]=>
string(74) "General, sporting, and business news for Malta and the surrounding region."
["url"]=>
string(22) "http://t.co/OYjxN0Y4tX"
["description"]=>
object(stdClass)#10 (1) {
["urls"]=>
array(0) {
}
}
}
["protected"]=>
bool(false)
["followers_count"]=>
int(13865)
["friends_count"]=>
int(13)
["listed_count"]=>
int(153)
["created_at"]=>
string(30) "Fri Oct 14 11:20:15 +0000 2011"
["favourites_count"]=>
int(0)
You can just simply do:
echo $array[0]->user->followers_count; //Returns 13865
Hey i wan't to echo something with belongsTo but the output gives me some strange characters back.
class Leviews extends Entity
{
public $table = 'reviews';
public function ravatar()
{
return $this->belongsTo('User', 'author', 'username')->select('avatar');
}
now when i echo the data with {{ $item->ravatar }} im getting this
{"avatar":"avatars\/1.jpg"}
and it should be
avatars/1.jpg
what im doing wrong?
edit
here is the Controller
<?php
use Carbon\Carbon;
use Lib\Reviews\LeviewsRepository;
use Lib\Services\Scraping\Scraper;
use Lib\Services\Validation\LeviewsValidator;
class LeviewsController extends \BaseController {
/**
* Leviews repository instance.
*
* #var Lib\Leviews\LeviewsRepository
*/
protected $repo;
/**
* validator instance.
*
* #var Lib\Services\Validation\LeviewsCreateValidator
*/
private $validator;
/**
* Leviews scraper isntance.
*
* #var Lib\Services\Scraping\NewScraper;
*/
private $scraper;
public function __construct(LeviewsRepository $lreviews, LeviewsValidator $validator, Scraper $scraper)
{
$this->beforeFilter('csrf', array('on' => 'post'));
$this->beforeFilter('logged', array('except' => array('index', 'show', 'paginate')));
$this->beforeFilter('news:create', array('only' => array('create', 'store')));
$this->beforeFilter('news:edit', array('only' => array('edit', 'update')));
$this->beforeFilter('news:delete', array('only' => 'destroy'));
$this->beforeFilter('news:update', array('only' => 'updateFromExternal'));
$this->repo = $lreviews;
$this->scraper = $scraper;
$this->validator = $validator;
}
/**
* Display list of paginated news.
*
* #return View
*/
public function index()
{
return View::make('Leviews.Index');
}
/**
* Display form for creating new news items.
*
* #return View
*/
public function create()
{
return View::make('Leviews.Create');
}
/**
* Store a newly created news item.
*
* #return Redirect
*/
public function store()
{
$input = Input::except('_token');
if ( ! $this->validator->with($input)->passes())
{
return Redirect::back()->withErrors($this->validator->errors())->withInput($input);
}
//escape double qoutes
$input['title'] = htmlspecialchars($input['title']);
$this->repo->store($input);
return Redirect::back()->withSuccess( trans('main.news create success') );
}
/**
* Display single news items.
*
* #param int $id
* #return View
*/
public function show($id)
{
$lreviews = $this->repo->byId($id);
if ($lreviews->full_url && ! $lreviews->fully_scraped)
{
$lreviews = $this->repo->getFullLeviewsItem($lreviews);
}
return View::make('Leviews.Show')->with(compact('news'))->withRecent($this->repo->latest());
}
/**
* Displays form for editing news item.
*
* #param int $id
* #return View
*/
public function edit($id)
{
$lreviews = $this->repo->byId($id);
return View::make('Leviews.Edit')->withLeviews($lreviews);
}
/**
* Updates the news item.
*
* #param int $id
* #return Redirect
*/
public function update($id)
{
$input = Input::except('_token', '_method');
$lreviews = $this->repo->byId($id);
if ($lreviews->title === $input['title'])
{
//dont check for title uniqueness when updating if
//title was not updated.
$this->validator->rules['title'] = 'required|min:2|max:255';
}
if ( ! $this->validator->with($input)->passes())
{
return Redirect::back()->withErrors($this->validator->errors())->withInput($input);
}
//escape double qoutes
$input['title'] = htmlspecialchars($input['title']);
$this->repo->update($lreviews, $input);
return Redirect::back()->withSuccess( trans('main.news update success') );
}
/**
* Delete specified news item.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
$this->repo->delete($id);
return Response::json(trans('main.news delete success'), 200);
}
/**
* Updates news from external sources.
*
* #return void
*/
public function updateFromExternal()
{
$this->scraper->updateLeviews();
Event::fire('Leviews.Updated', Carbon::now());
return Redirect::back()->withSuccess( trans('dash.updated news successfully') );
}
}
and here the view
#if ($options->enableNews())
#foreach($lreviews as $k => $item)
{{ $item->body }}
{{ $item->ravatar }}
#endforeach
#endif
{{ $item->body }} is without strange characters
edit
here the dd output
object(Illuminate\Database\Eloquent\Collection)#634 (1) { ["items":protected]=> array(6) { [0]=> object(Leviews)#606 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(42) ["author"]=> string(7) "Clayman" ["source"]=> string(8) "Japanime" ["body"]=> string(182) "Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review " ["score"]=> int(9) ["link"]=> NULL ["title_id"]=> int(1655) ["created_at"]=> string(19) "2015-04-24 16:08:34" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(1) ["type"]=> string(4) "user" } ["original":protected]=> array(12) { ["id"]=> int(42) ["author"]=> string(7) "Clayman" ["source"]=> string(8) "Japanime" ["body"]=> string(182) "Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review " ["score"]=> int(9) ["link"]=> NULL ["title_id"]=> int(1655) ["created_at"]=> string(19) "2015-04-24 16:08:34" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(1) ["type"]=> string(4) "user" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [1]=> object(Leviews)#635 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(1) ["author"]=> string(18) "James Berardinelli" ["source"]=> string(9) "ReelViews" ["body"]=> string(265) "The movie is pretty to look at in a "Transformers" sort of way and moves briskly enough that it never threatens to bore, but it's hard to feel much of anything about the characters and, when it's all over, there's a sense that everything that happens is obligatory." ["score"]=> int(63) ["link"]=> string(64) "http://www.reelviews.net/php_review_template.php?identifier=2687" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(1) ["author"]=> string(18) "James Berardinelli" ["source"]=> string(9) "ReelViews" ["body"]=> string(265) "The movie is pretty to look at in a "Transformers" sort of way and moves briskly enough that it never threatens to bore, but it's hard to feel much of anything about the characters and, when it's all over, there's a sense that everything that happens is obligatory." ["score"]=> int(63) ["link"]=> string(64) "http://www.reelviews.net/php_review_template.php?identifier=2687" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [2]=> object(Leviews)#629 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(2) ["author"]=> string(10) "Steven Rea" ["source"]=> string(21) "Philadelphia Inquirer" ["body"]=> string(345) "One of the problems with The Dark World is that its monsters and angry armies and visual effects are interchangeable with Peter Jackson's Tolkien pics, with Clash of the Titans, with The Avengers, with Man of Steel, and on and on. These superhero movies. These Middle Earth movies. These mythic god movies. It's getting hard to tell them apart." ["score"]=> int(63) ["link"]=> string(99) "http://www.philly.com/philly/entertainment/movies/20131108_Thor_s_back__more_generic_than_ever.html" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(2) ["author"]=> string(10) "Steven Rea" ["source"]=> string(21) "Philadelphia Inquirer" ["body"]=> string(345) "One of the problems with The Dark World is that its monsters and angry armies and visual effects are interchangeable with Peter Jackson's Tolkien pics, with Clash of the Titans, with The Avengers, with Man of Steel, and on and on. These superhero movies. These Middle Earth movies. These mythic god movies. It's getting hard to tell them apart." ["score"]=> int(63) ["link"]=> string(99) "http://www.philly.com/philly/entertainment/movies/20131108_Thor_s_back__more_generic_than_ever.html" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [3]=> object(Leviews)#630 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(3) ["author"]=> string(12) "Simon Abrams" ["source"]=> string(14) "RogerEbert.com" ["body"]=> string(133) "Thor: The Dark World's characters are often very charming, but they're only so much fun when they're stuck going through the motions." ["score"]=> int(63) ["link"]=> string(58) "http://www.rogerebert.com/reviews/thor-the-dark-world-2013" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(3) ["author"]=> string(12) "Simon Abrams" ["source"]=> string(14) "RogerEbert.com" ["body"]=> string(133) "Thor: The Dark World's characters are often very charming, but they're only so much fun when they're stuck going through the motions." ["score"]=> int(63) ["link"]=> string(58) "http://www.rogerebert.com/reviews/thor-the-dark-world-2013" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [4]=> object(Leviews)#641 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(4) ["author"]=> string(10) "Mike Scott" ["source"]=> string(26) "New Orleans Times-Picayune" ["body"]=> string(213) "None of that is to say that Thor: The Dark World is a bad movie, necessarily. I would never speak ill of a man with a giant, magical hammer. At the same time, hammer or no hammer, it doesn't quite nail it, either." ["score"]=> int(40) ["link"]=> string(95) "http://www.nola.com/movies/index.ssf/2013/11/thor_the_dark_world_movie_revi.html#incart_m-rpt-1" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(4) ["author"]=> string(10) "Mike Scott" ["source"]=> string(26) "New Orleans Times-Picayune" ["body"]=> string(213) "None of that is to say that Thor: The Dark World is a bad movie, necessarily. I would never speak ill of a man with a giant, magical hammer. At the same time, hammer or no hammer, it doesn't quite nail it, either." ["score"]=> int(40) ["link"]=> string(95) "http://www.nola.com/movies/index.ssf/2013/11/thor_the_dark_world_movie_revi.html#incart_m-rpt-1" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [5]=> object(Leviews)#640 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(5) ["author"]=> string(12) "Peter Rainer" ["source"]=> string(25) "Christian Science Monitor" ["body"]=> string(183) "My favorite moment in the movie: Astrophysicist Erik Selvig (Stellan Skarsgard) insisting on wearing only his underwear because he says he thinks better that way. Hey, whatever works." ["score"]=> int(58) ["link"]=> string(108) "http://www.csmonitor.com/The-Culture/Movies/2013/1108/Thor-The-Dark-World-has-lost-the-spark-of-the-original" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(5) ["author"]=> string(12) "Peter Rainer" ["source"]=> string(25) "Christian Science Monitor" ["body"]=> string(183) "My favorite moment in the movie: Astrophysicist Erik Selvig (Stellan Skarsgard) insisting on wearing only his underwear because he says he thinks better that way. Hey, whatever works." ["score"]=> int(58) ["link"]=> string(108) "http://www.csmonitor.com/The-Culture/Movies/2013/1108/Thor-The-Dark-World-has-lost-the-spark-of-the-original" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } } }
edit
array(6) { [0]=> array(12) { ["id"]=> int(42) ["author"]=> string(7) "Clayman" ["source"]=> string(8) "Japanime" ["body"]=> string(182) "Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review " ["score"]=> int(9) ["link"]=> NULL ["title_id"]=> int(1655) ["created_at"]=> string(12) "Apr 24, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(1) ["type"]=> string(4) "user" } [1]=> array(12) { ["id"]=> int(1) ["author"]=> string(18) "James Berardinelli" ["source"]=> string(9) "ReelViews" ["body"]=> string(265) "The movie is pretty to look at in a "Transformers" sort of way and moves briskly enough that it never threatens to bore, but it's hard to feel much of anything about the characters and, when it's all over, there's a sense that everything that happens is obligatory." ["score"]=> int(63) ["link"]=> string(64) "http://www.reelviews.net/php_review_template.php?identifier=2687" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [2]=> array(12) { ["id"]=> int(2) ["author"]=> string(10) "Steven Rea" ["source"]=> string(21) "Philadelphia Inquirer" ["body"]=> string(345) "One of the problems with The Dark World is that its monsters and angry armies and visual effects are interchangeable with Peter Jackson's Tolkien pics, with Clash of the Titans, with The Avengers, with Man of Steel, and on and on. These superhero movies. These Middle Earth movies. These mythic god movies. It's getting hard to tell them apart." ["score"]=> int(63) ["link"]=> string(99) "http://www.philly.com/philly/entertainment/movies/20131108_Thor_s_back__more_generic_than_ever.html" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [3]=> array(12) { ["id"]=> int(3) ["author"]=> string(12) "Simon Abrams" ["source"]=> string(14) "RogerEbert.com" ["body"]=> string(133) "Thor: The Dark World's characters are often very charming, but they're only so much fun when they're stuck going through the motions." ["score"]=> int(63) ["link"]=> string(58) "http://www.rogerebert.com/reviews/thor-the-dark-world-2013" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [4]=> array(12) { ["id"]=> int(4) ["author"]=> string(10) "Mike Scott" ["source"]=> string(26) "New Orleans Times-Picayune" ["body"]=> string(213) "None of that is to say that Thor: The Dark World is a bad movie, necessarily. I would never speak ill of a man with a giant, magical hammer. At the same time, hammer or no hammer, it doesn't quite nail it, either." ["score"]=> int(40) ["link"]=> string(95) "http://www.nola.com/movies/index.ssf/2013/11/thor_the_dark_world_movie_revi.html#incart_m-rpt-1" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [5]=> array(12) { ["id"]=> int(5) ["author"]=> string(12) "Peter Rainer" ["source"]=> string(25) "Christian Science Monitor" ["body"]=> string(183) "My favorite moment in the movie: Astrophysicist Erik Selvig (Stellan Skarsgard) insisting on wearing only his underwear because he says he thinks better that way. Hey, whatever works." ["score"]=> int(58) ["link"]=> string(108) "http://www.csmonitor.com/The-Culture/Movies/2013/1108/Thor-The-Dark-World-has-lost-the-spark-of-the-original" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } }
$item->ravatar is model and not just a string. What you see is the JSON representation of that model. To get the actual path use:
{{ $item->ravatar->avatar }}
If it's possible that not every review has has an avatar, check that with an if statement or use this:
{{ $item->ravatar->avatar or 'avatar/default.jpg' }}
so I'm using a PHP Twitter API to grab a list of tweets with a certain hashtag. I'm looking to get the following info from the returned json; 'screen_name' and 'text'.
The screen_name is on line 44 and the text is on line 20.
Here is the json output.
object(stdClass)#5 (2) {
["statuses"]=>
array(15) {
[0]=>
object(stdClass)#6 (24) {
["metadata"]=>
object(stdClass)#7 (2) {
["iso_language_code"]=>
string(2) "en"
["result_type"]=>
string(6) "recent"
}
["created_at"]=>
string(30) "Fri Dec 05 21:22:00 +0000 2014"
["id"]=>
float(5.4097942452372E+17)
["id_str"]=>
string(18) "540979424523718656"
["text"]=>
string(100) "Shits getting real. Crit happens tonight. #dnd #dungeonsanddragons #nerd #rpg http://t.co/44Lnrmmfte"
["source"]=>
string(59) "Instagram"
["truncated"]=>
bool(false)
["in_reply_to_status_id"]=>
NULL
["in_reply_to_status_id_str"]=>
NULL
["in_reply_to_user_id"]=>
NULL
["in_reply_to_user_id_str"]=>
NULL
["in_reply_to_screen_name"]=>
NULL
["user"]=>
object(stdClass)#8 (41) {
["id"]=>
int(1297196190)
["id_str"]=>
string(10) "1297196190"
["name"]=>
string(12) "Brandon Wade"
["screen_name"]=>
string(15) "Brandonus_Prime"
["location"]=>
string(15) "The woods of NC"
["profile_location"]=>
NULL
["description"]=>
string(29) "If it bleeds, we can kill it."
["url"]=>
string(22) "http://t.co/EkGUdm9yDO"
["entities"]=>
object(stdClass)#9 (2) {
["url"]=>
object(stdClass)#10 (1) {
["urls"]=>
array(1) {
[0]=>
object(stdClass)#11 (4) {
["url"]=>
string(22) "http://t.co/EkGUdm9yDO"
["expanded_url"]=>
string(34) "http://facebook.com/brandonusprime"
["display_url"]=>
string(27) "facebook.com/brandonusprime"
["indices"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(22)
}
}
}
}
["description"]=>
object(stdClass)#12 (1) {
["urls"]=>
array(0) {
}
}
}
["protected"]=>
bool(false)
["followers_count"]=>
int(44)
["friends_count"]=>
int(246)
["listed_count"]=>
int(0)
["created_at"]=>
string(30) "Mon Mar 25 01:10:47 +0000 2013"
["favourites_count"]=>
int(18)
["utc_offset"]=>
NULL
["time_zone"]=>
NULL
["geo_enabled"]=>
bool(false)
["verified"]=>
bool(false)
["statuses_count"]=>
int(170)
["lang"]=>
string(2) "en"
["contributors_enabled"]=>
bool(false)
["is_translator"]=>
bool(false)
["is_translation_enabled"]=>
bool(false)
["profile_background_color"]=>
string(6) "C0DEED"
["profile_background_image_url"]=>
string(48) "http://abs.twimg.com/images/themes/theme1/bg.png"
["profile_background_image_url_https"]=>
string(49) "https://abs.twimg.com/images/themes/theme1/bg.png"
["profile_background_tile"]=>
bool(false)
["profile_image_url"]=>
string(99) "http://pbs.twimg.com/profile_images/378800000698263057/6592d36cf8e2beb02c9f3ee9abf4fdd7_normal.jpeg"
["profile_image_url_https"]=>
string(100) "https://pbs.twimg.com/profile_images/378800000698263057/6592d36cf8e2beb02c9f3ee9abf4fdd7_normal.jpeg"
["profile_banner_url"]=>
string(59) "https://pbs.twimg.com/profile_banners/1297196190/1364236862"
["profile_link_color"]=>
string(6) "0084B4"
["profile_sidebar_border_color"]=>
string(6) "C0DEED"
["profile_sidebar_fill_color"]=>
string(6) "DDEEF6"
["profile_text_color"]=>
string(6) "333333"
["profile_use_background_image"]=>
bool(true)
["default_profile"]=>
bool(true)
["default_profile_image"]=>
bool(false)
["following"]=>
bool(false)
["follow_request_sent"]=>
bool(false)
["notifications"]=>
bool(false)
}
["geo"]=>
NULL
["coordinates"]=>
NULL
["place"]=>
NULL
["contributors"]=>
NULL
["retweet_count"]=>
int(0)
["favorite_count"]=>
int(0)
["entities"]=>
object(stdClass)#13 (4) {
["hashtags"]=>
array(4) {
[0]=>
object(stdClass)#14 (2) {
["text"]=>
string(3) "dnd"
["indices"]=>
array(2) {
[0]=>
int(42)
[1]=>
int(46)
}
}
[1]=>
object(stdClass)#15 (2) {
["text"]=>
string(18) "dungeonsanddragons"
["indices"]=>
array(2) {
[0]=>
int(47)
[1]=>
int(66)
}
}
[2]=>
object(stdClass)#16 (2) {
["text"]=>
string(4) "nerd"
["indices"]=>
array(2) {
[0]=>
int(67)
[1]=>
int(72)
}
}
[3]=>
object(stdClass)#17 (2) {
["text"]=>
string(3) "rpg"
["indices"]=>
array(2) {
[0]=>
int(73)
[1]=>
int(77)
}
}
}
["symbols"]=>
array(0) {
}
["user_mentions"]=>
array(0) {
}
["urls"]=>
array(1) {
[0]=>
object(stdClass)#18 (4) {
["url"]=>
string(22) "http://t.co/44Lnrmmfte"
["expanded_url"]=>
string(34) "http://instagram.com/p/wPV3gmr1kl/"
["display_url"]=>
string(27) "instagram.com/p/wPV3gmr1kl/"
["indices"]=>
array(2) {
[0]=>
int(78)
[1]=>
int(100)
}
}
}
}
["favorited"]=>
bool(false)
["retweeted"]=>
bool(false)
["possibly_sensitive"]=>
bool(false)
["lang"]=>
string(2) "en"
}
}
Here is my PHP Code
<?php
require_once 'twitteroauth/twitteroauth.php';
define('CONSUMER_KEY', '');
define('CONSUMER_SECRET', '');
define('ACCESS_TOKEN', '-');
define('ACCESS_TOKEN_SECRET', '');
$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$query = array(
"q" => "#nerd",
"result_type" => "recent"
);
$results = $toa->get('search/tweets', $query);
var_dump($results); die();
foreach ($results->statuses as $result) {
echo $result->user->screen_name . ": " . $result->text . "<br>";
}
Thanks a ton! I've been trying for a while and need some pointing in the right direction :)
This is my first post so hello there all!
Wondering could I get a bit of help with getting my results out of this md array? Its giving me unexpected results. I have looked for answers but am not sure what question I should be asking..
This is a sample of the relevant part of the array [using var_dump]:
["Attendee"] => array(11)
{
["id"]=> int(148)
["firstname"]=> string(5) "dave"
["lastname"]=> string(6) "davey"`
}
This is my foreach code to get the first and last names for each attendee:
foreach ($the_info_array['body']['Registrations'] as $registrations)
{
foreach ($registrations as $regvalue)
{
echo $regvalue['firstname'].” “.$regvalue['lastname'];
}
echo “<br>”;
}
This prints:
a a2 2 5 5 dave davey
a a2 2 5 5 scott davey
a a2 2 5 5 bill davey
etc
rather than what i want ie:
dave davey
scott davey
bill davey
Where am i going wrong, & what are the extra characters?
Thanks in advance for any kind help the community can give!
EDIT>>
Strangely, if I change the echo to
echo $regvalue['firstname'];
it prints:
a25dave
a25scott
a25bill
and
echo $regvalue['firstname']." ";
prints
a 2 5 dave
a 2 5 scott
a 2 5 bill
Here is the complete array minus several records as I can't seem to post that many characters:
array(3) {
["status"]=> string(2) "OK"
["status_code"]=> int(200)
["body"]=> array(1) {
["Registrations"]=> array(12) {
[0]=> array(15) {
["id"]=> int(148)
["status"]=> string(8) "approved"
["date_of_registration"]=> string(19) "2013-12-04 12:43:31"
["final_price"]=> int(0)
["code"]=> string(23) "529f2373bb7555.08033147"
["url_link"]=> string(0) ""
["is_primary"]=> bool(true)
["is_group_registration"]=> bool(true)
["is_going"]=> bool(true)
["is_checked_in"]=> bool(false)
["Event"]=> array(14) {
["id"]=> int(531) ["code"]=> string(15) "1-5298c3675e610"
["name"]=> string(21) "Recital 2014"
["description"]=> string(721) "The Description is this!"
["status"]=> string(6) "active"
["limit"]=> int(280)
["group_registrations_allowed"]=> bool(true)
["group_registrations_max"]=> int(50)
["active"]=> bool(true)
["member_only"]=> bool(false)
["virtual_url"]=> string(0) ""
["call_in_number"]=> string(0) ""
["phone"]=> string(0) ""
["metadata"]=> array(9) {
["default_payment_status"]=> string(10) "Incomplete"
["venue_id"]=> int(1)
["additional_attendee_reg_info"]=> string(1) "1"
["add_attendee_question_groups"]=> array(1) {
[1]=> string(1) "1" }
["date_submitted"]=> string(10) "29/11/2013"
["event_hashtag"]=> string(0) ""
["event_format"]=> string(0) ""
["event_livestreamed"]=> string(0) ""
[""]=> string(0) "" } }
["Attendee"]=> array(11) {
["id"]=> int(219)
["firstname"]=> string(5) “dave”
[“lastname”]=> string(7) "davis"
["address"]=> string(13) "10 The Street"
["address2"]=> string(0) ""
["city"]=> string(8) "The City"
["state"]=> string(10) "The County"
["country"]=> string(0) ""
["zip"]=> string(6) "BN66YY"
["email"]=> string(17) "mail#webhost.com"
["phone"]=> string(12) "012736654432" }
["Transaction"]=> array(9) {
["id"]=> int(219)
["timestamp"]=> string(19) "2013-12-23 00:26:22"
["total"]=> float(132.6)
["amount_paid"]=> int(0)
["status"]=> string(10) "incomplete"
["details"]=> string(0) ""
["tax_data"]=> string(0) ""
["session_data"]=> string(0) ""
["payment_gateway"]=> string(19) "Credit / Debit Card" }
["Datetime"]=> array(8) {
["id"]=> int(0) ["is_primary"]=> bool(true)
["event_start"]=> string(19) "2014-01-11 00:00:00"
["event_end"]=> string(19) "2014-01-11 00:00:00"
["registration_start"]=> string(19) "2012-08-12 16:00:00"
["registration_end"]=> string(19) "2014-01-10 16:00:00"
["limit"]=> int(280) ["tickets_left"]=> int(274) }
["Price"]=> array(9) {
["id"]=> int(0)
["name"]=> string(0) ""
["amount"]=> int(0)
["description"]=> string(0) ""
["limit"]=> int(9999999)
["remaining"]=> int(999999)
["start_date"]=> NULL
["end_date"]=> NULL
["Pricetype"]=> array(8) {
["id"]=> int(1)
["name"]=> string(10) "Base Price"
["is_member"]=> bool(false)
["is_discount"]=> bool(false)
["is_tax"]=> bool(false)
["is_percent"]=> bool(false)
["is_global"]=> bool(true)
["order"]=> int(0) } } } } } }
// Also, don't know if relevant, but the array comes from a json decode originally
The problem is that your looping over each array inside registrations even though most of these are not an attendee (i.e $regvalue will be an id in the first loop status in the second loop e.t.c.).
Try:
foreach ($the_info_array['body']['Registrations'] as $registrations)
{
echo $registrations['Attendee']['firstname']." ".$registrations['Attendee']['lastname'];
echo "<br>";
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
From the following response from Twitter, how can I access 'followers_count'?
array(1) {
[0]=> object(stdClass)#1 (21) {
["created_at"]=> string(30) "Mon Oct 15 19:20:28 +0000 2012"
["id"]=> int(257923917879070721)
["id_str"]=> string(18) "257923917879070721"
["text"]=> string(127) "As open enrollment begins, Medicare is getting stronger – but Rep. Ryan wants to dismantle it. My op-ed: http://t.co/DUEYxQOt"
["source"]=> string(3) "web"
["truncated"]=> bool(false)
["in_reply_to_status_id"]=> NULL
["in_reply_to_status_id_str"]=> NULL
["in_reply_to_user_id"]=> NULL
["in_reply_to_user_id_str"]=> NULL
["in_reply_to_screen_name"]=> NULL
["user"]=> object(stdClass)#2 (38) {
["id"]=> int(16789970)
["id_str"]=> string(8) "16789970"
["name"]=> string(18) "Senator Harry Reid"
["screen_name"]=> string(11) "SenatorReid"
["location"]=> string(15) "Searchlight, NV"
["url"]=> string(22) "http://reid.senate.gov"
["description"]=> string(108) "News from the office of Nevada Senator Harry Reid, the U.S. Senate Majority Leader. (Español: #SenadorReid)"
["protected"]=> bool(false)
["followers_count"]=> int(67261)
["friends_count"]=> int(126)
["listed_count"]=> int(3712)
["created_at"]=> string(30) "Wed Oct 15 19:42:08 +0000 2008"
["favourites_count"]=> int(1)
["utc_offset"]=> int(-18000)
["time_zone"]=> string(5) "Quito"
["geo_enabled"]=> bool(false)
["verified"]=> bool(true)
["statuses_count"]=> int(1556)
["lang"]=> string(2) "en"
["contributors_enabled"]=> bool(false)
["is_translator"]=> bool(false)
["profile_background_color"]=> string(6) "233E6D"
["profile_background_image_url"]=> string(70) "http://a0.twimg.com/profile_background_images/4744650/reid_twitter.jpg"
["profile_background_image_url_https"]=> string(72) "https://si0.twimg.com/profile_background_images/4744650/reid_twitter.jpg"
["profile_background_tile"]=> bool(false)
["profile_image_url"]=> string(66) "http://a0.twimg.com/profile_images/81906216/reid_square_normal.jpg"
["profile_image_url_https"]=> string(68) "https://si0.twimg.com/profile_images/81906216/reid_square_normal.jpg"
["profile_banner_url"]=> string(63) "https://si0.twimg.com/brand_banners/SenatorReid/1323385337/live"
["profile_link_color"]=> string(6) "004CB4"
["profile_sidebar_border_color"]=> string(6) "6383A3"
["profile_sidebar_fill_color"]=> string(6) "AFC4DA"
["profile_text_color"]=> string(6) "333333"
["profile_use_background_image"]=> bool(true)
["default_profile"]=> bool(false)
["default_profile_image"]=> bool(false)
["following"]=> NULL
["follow_request_sent"]=> NULL
["notifications"]=> NULL } ["geo"]=> NULL
["coordinates"]=> NULL
["place"]=> NULL
["contributors"]=> NULL
["retweet_count"]=> int(88)
["entities"]=> object(stdClass)#3 (3) {
["hashtags"]=> array(0) { }
["urls"]=> array(1) {
[0]=> object(stdClass)#4 (4) {
["url"]=> string(20) "http://t.co/DUEYxQOt"
["expanded_url"]=> string(21) "http://huff.to/TshePO"
["display_url"]=> string(14) "huff.to/TshePO"
["indices"]=> array(2) {
[0]=> int(105)
[1]=> int(125)
}
}
}
["user_mentions"]=> array(0) { }
}
["favorited"]=> bool(false)
["retweeted"]=> bool(false)
["possibly_sensitive"]=> bool(false)
}
}
To read easly your var_dump, you can install x-debug : http://xdebug.org/.
And to access at your information, it's :
$followersCount = $twitterResponse[0]->user->followers_count
for your example.
$response[0]["user"]["followers_count"]