Im trying to navigate and fetch info from this array but im not having much success any tips? This is an decoded JSON from Twitter API
I tryed echo $array->statuses[0]->created_at; and it did not work, am i doing something wrong here?
Array
(
[statuses] => Array
(
[0] => stdClass Object
(
[created_at] => Thu Jul 06 04:05:06 +0000 2017
[id] => 882812623959584768
[id_str] => 882812623959584768
[text] => RT #blackdragonsBR: [R6] GG!! Fechamos o mapa Banco por 5x2 e conquistamos a liderança no #ALIENWARENOIGN #GOBD #Acer #Predator https://t.c…
[truncated] =>
[entities] => stdClass Object
(
[hashtags] => Array
(
[0] => stdClass Object
(
[text] => ALIENWARENOIGN
[indices] => Array
(
[0] => 90
[1] => 105
}
)
}
}
}
}
}
You need to access the array by [] not ->. So change the access of statuses like below
$array[statuses][0]->created_at;
You can use json_encode and json_decode at time then it make the variable to array only then you can get the data easily.
Just like this
$array=json_decode(json_encode($array,true),true);
$array[statuses][0]['created_at'];
It will work for you
Related
I am using the Twitter API with this code:
$statuses = $connection->get("favorites/list", ["screen_name" => "Ali" , "count" => 2 ]);
print_r ($statuses);
Which gives this result:
Array ( [0] => stdClass Object ( [created_at] => Sun Jun 28 04:56:55 +0000 2020 [id] => 1277103662918238210 [id_str] => 1277103662918238210 [text] => #mu11igan #LAGallday #ali The Church has always been tied to buildings. The Temple (here and the one in heaven.),sy… [truncated] => 1 [entities] => stdClass Object ( [hashtags] => Array ( ) [symbols] => Array ( ) [user_mentions] => Array ( [0] => stdClass Object
But I would like to display only one specific element;
for example only this one [id_str] => 1277103662918238210
You can just do simply like this.
$retValue = {};
$retValue['id_str'] = $statuses[0][id_str]; // or $statuses[0]->id_str
print_r($retValue); // or echo(json_encode($retValue))
Hope this helps you to understand.
Okay.. I did a lot of research but I kept getting errors so I decided to ask the question directly..
I always converted objects to arrays, but I now want to try using an object directly to save website speed.. I got this object:
stdClass Object
(
[date] => Wed, 8 Feb 2017 15:03:44 +0000
[Date] => Wed, 8 Feb 2017 15:03:44 +0000
[subject] => asdasdasd
[Subject] => asdasdasd
[message_id] =>
[toaddress] => "test#hotmail.com"
[to] => Array
(
[0] => stdClass Object
(
[personal] => test#hotmail.com
[mailbox] => test
[host] => hotmail.com
)
)
[fromaddress] => Jason K
[from] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[reply_toaddress] => Jason K
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[senderaddress] => Jason K
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[Recent] =>
[Unseen] =>
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 1
[MailDate] => 8-Feb-2017 16:03:25 +0100
[Size] => 7887
[udate] => 1486566205
)
I am trying to get the first date (wed, 8 feb 2017), the from->mailbox and the Size. I managed to get the from->mailbox with this code:
foreach($EmailHeaders->from as $from ){
echo $from->mailbox;
}
But I just cant find out how to obtain the other values aswell.. If i try:
foreach($EmailHeaders as $headers){
echo $headers->date;
}
then it doesnt work... Can anyone explain this to me? Sorry if this is already asked a thousand times before, I just cant figure it out..
check this http://php.net/manual/en/function.get-object-vars.php
var_dump(get_object_vars($EmailHeaders ));
It's simple, you don't have to iterate over the direct properties, just use
$EmailHeaders->date
To get the "date", you just need to use $EmailHeaders->date
You don't need a foreach. It's working for "from" because it contains an array of objects.
You can simply do
$EmailHeaders->date
OR
$EmailHeaders->Date
to get the date from your object.
While iterating through loop, your pointer is already on the date item, so you can't point to date index. It will point to the date index under date item.
I am trying to fetch data from user's twitter account with this code
$user_info = $twitteroauth->get('account/verify_credentials');
i get a very long value in an object
stdClass Object
(
[id] => 2999359418
[id_str] => 2999359418
[name] => ABC
[screen_name] => ABC9
[location] =>
[profile_location] =>
[description] => XYZ
[url] =>
[entities] => stdClass Object
(
[description] => stdClass Object
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 0
[friends_count] => 0
[listed_count] => 0
[created_at] => Wed Jan 28 12:17:43 +0000 2015
)
i wish to retrieve data from it.
eg. i tried to retrieve description by this code but did it wrong, coz i did not get the result. can anyone please tell how to fetch the data
The information is not an array, but an object. You can access the data by the following:
$info = $user_info->description;
You can repeat this by to go further down the chain such as:
$user_info->entities->description, etc.
You are trying to access a object with the array index syntax it will not work this way.
When you are dealing with objects you need to access the array by using "->"
$user_info->name;
$user_info->entities->description;
$user_info->entities->description->urls;
This is the pattern to access to the depth
I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);
Im kinda new with php and JSON, and after digging around for a response I was not able to find one, I was hoping if you could help me…
I am working on a Report from with a JSON Response
The JSON response looks somewhat like this:
stdClass Object
(
[FlightInformation] => stdClass Object
(
[#size] => 10
[Itinerary] => Array
(
[0] => stdClass Object
(
[bookingID] => 123456789
[creationDate] => 10/04/2012
[Customer] => stdClass Object
(
[email] => someone#aol.com
)
[FlightConfirmation] => stdClass Object
(
[supplierId] => AA
[arrivalDate] => 10/04/2012
[departureDate] => 10/05/2012
[confirmationNumber] => 0987654321
[RateInformation] => stdClass Object
(
[#size] => 1
[RateDescription] => stdClass Object
(
[#promo] => false
[ChargeableInfo] => stdClass Object
(
[#total] => 57.94
I building a Report that is like this..
foreach( $response->FlightInformation->Itinerary as $output) {
echo $output-> bookingID;
echo $output-> creationDate;
echo $output-> arrivalDate; <<<< won't Print
echo $output->departureDate; <<<< won't Print
echo $output->total; <<<< won't Print
and some more elements… but anything that is under an object inside de Itinerary Array won't show..
I cannot get to print thes values indicated, please help…
Cheers
That is because arrival, departure and total are objects themselves. You would have to do something like:
$output->Customer->FlightConfirmation->arrivalDate;
$output->Customer->FlightConfirmation->departureDate
$output->Customer->FlightConfirmation->RateInformation->RateDescription->ChargeableInfo->total;
Your missing a level of nesting those elements are inside FlightConfirmation: