looping through object in php gives error - php

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.

Related

How to select a specific property from array

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.

Accessing ddeboer/Imap Mail Header

I'm accessing a mailbox using ddeboer/imap. Connecting to the Server and retrieving Messages ist not a problem. But $message->getHeaders() returns the following (shortened Version):
Ddeboer\Imap\Message\Headers Object
(
[storage:ArrayIterator:private] => Array
(
[date] => Fri, 5 Jul 2019 07:00:47 +0200
[subject] => Test Mail
[message_id] => <108a4850-284e-170a-2c7d-b6f9g5218202#test.de>
[fromaddress] => "Test" <test#test.de>
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test
[mailbox] => test
[host] => test.de
)
)
[deleted] =>
[draft] =>
[msgno] => 1
[maildate] => 5-Jul-2019 07:00:49 +0200
[size] => 223715
[udate] => 1562302849
)
)
How am I supposed to access the Information e.g. udate or from->mailbox?
You can use the get method and add the path
$message->getHeaders()->get('udate'):
$message->getHeaders()->get('from')[0]->mailbox

how to get value from this php array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
i have this array from json_decode
stdClass Object (
[coord] => stdClass Object (
[lon] => 8.97
[lat] => 51
)
[weather] => Array (
[0] => stdClass Object (
[id] => 804
[main] => Clouds
[description] => overcast clouds
[icon] => 04d
)
)
[base] => stations
[main] => stdClass Object (
[temp] => 281.17
[pressure] => 1014
[humidity] => 87
[temp_min] => 280.93
[temp_max] => 281.48
)
[visibility] => 7000
[wind] => stdClass Object (
[speed] => 3.6
[deg] => 180
)
[clouds] => stdClass Object (
[all] => 90
)
[dt] => 1445499854
[sys] => stdClass Object (
[type] => 1
[id] => 4954
[message] => 0.0056
[country] => DE
[sunrise] => 1445493535
[sunset] => 1445530644
)
[id] => 2906244
[name] => Herbelhausen
[cod] => 200
)
i have get from it the description
$response_a2->weather[0]->description
but wen am trying to get the temp or pressure or humidity or speed i filed
$response_a2->base->temp
You are looking for
$response_a2->main->temp
Thanks to RiggsFolly for beautifying the array.
next time, write your code like this:
echo "<pre">;
print_r($response_a2);
echo "</pre>";
and your array will look beautiful in the browser
That data does not exist in $response_a2->base->temp
Instead us
$response_a2->main->temp
$response_a2->main->presure
$response_a2->main->humidity
etc
What you are showing us is an object, not an array. Objects work differently from arrays. They use the -> operator to reach certain values.
For example, imagine I've got object $obj and I wanna reach value ['some_value'].
echo $obj->some_value;

Retrieve email address from imap header

I'm using php imap function imap_header to extract full header and imap_fetch_overview to extract raw header. Both the functions gives me stdClass object and arrays respectively.
I would like to always clean my from and to before further processing. Sometimes a FROM or TO can contain some thing like this,
Test user <test#test.com>
Also currently in my FROM I only see Test User and no email address until I use firebug.
How do I just get the test#test.com from these objects, arrays?
This is the result I get from imap_fetch_overview
Array
(
[0] => stdClass Object
(
[subject] => Testing
[from] => Test User
[to] => testuser2#test.com
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[message_id] => <abcdef1244.93784jgsfk#test.com>
[size] => 3443
[uid] => 1234
[msgno] => 123
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1366204439
)
)
There is a hidden <test#test.com> next to Test User. How do I extract that email address?
Similarly this is what I get from imap_header
stdClass Object
(
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[Date] => Wed, 17 Apr 2013 18:43:46 +0530
[subject] => Test
[Subject] => Test
[message_id] => <abcdef1244.93784jgsfk#test.com>
[toaddress] => testuser#test.com
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => testuser
[host] => test.com
)
)
[fromaddress] => Test User
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[reply_toaddress] => Test User
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[senderaddress] => Test User
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[Recent] =>
[Unseen] => U
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 123
[MailDate] => 17-Apr-2013 13:13:59 +0000
[Size] => 3443
[udate] => 1366204439
)
A preg_match would be the obvious answer but just cant seem to figure out how to perform it on from bit where email address is not seen in the browser but is present when inspected with firebug. Any help is appreciated.
Thanks.
You can go one step further with imap_rfc822_parse_adrlist()Docs:
$toAddresses = imap_rfc822_parse_adrlist('Test user <test#test.com>', 'localhost');
print_r($toAddresses);
Array
(
[0] => stdClass Object
(
[mailbox] => test
[host] => test.com
[personal] => Test user
)
)
If name of array of std object is $arr
$arr[0]->to
returns what you want.
in second case
$stdObject <- object that is returned by second function
foreach($stdObject->to as $to)
{
echo $to->mailbox;
echo $to->host;
}
I've just discovered this thread and whilst it's old I hope this comment might help. I believe I've answered the query. As it happens, the format of the sender email is what's causing the issue.
From Name <from#name.com>
Because of the <> tag, the browser is making it into an HTML tag, I guess something akin to a span. By using str_replace to change "<" into " " (or whatever) you remove the issue before it gets to HTML

Access array elements from API

For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.
Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?
I also tried this with no luck.
$object = $buy[1];
$title = $object->title[0];
echo "$title";
Thanks
This is what the api gives me.
stdClass Object
(
[id] => 1
[name] => toronto
[date_modified] => 2011-03-08 13:07:10
[tax_rate_provincial] =>
)
<br/>
Array
(
[0] => stdClass Object
(
[0] => stdClass Object
(
[id] => 28131844
[full_date] => 20110506
[end_date] => 20110511
[city] => toronto
[saved] => 1651
[discount_percentage] => 52
[deal_option] => Array
(
[0] => stdClass Object
(
[id] => 2600
[title] =>
[date_modified] => 0000-00-00 00:00:00
[date_created] => 2011-05-03 14:33:58
[value] => 3150
[price] => 1499
[deal_id] => 28131844
[is_default] => 0
)
)
[options] =>
[option_quantity] =>
[option_remaining] =>
[purchase_limit] => 1
[gift_limit] => 0
There is a special evil syntax to bypass numeric object attributes:
print $obj->{'0'}->{'0'}->city;
Is the correct syntax, and equivalent to the path you already determined.
Your second example is an array however, so it's probably:
print $array[0]->{'0'}->city;
The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.

Categories