如何从 PHP 中的 stdClass 获取值?
How to fetch the values from stdClass in PHP?
我正在 php 中搜索 stdClass 数组中的值。我很难从数组中获取值,因为当我尝试使用 ifdisposition
和 disposition == 'inline'
搜索值时,它不会获取值并存储在 $body_attachment
数组中我要。
当我尝试这个时:
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition == 'inline')) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $object->value;
}
}
$body_number++;
}
它将输出如下:
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => RELATED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 29154
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => what-is-bootstrap.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
)
)
)
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => RELATED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => 000000000000098145058c769850
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => ALTERNATIVE
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => 000000000000098144058c76984f
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 0
[ifsubtype] => 1
[subtype] => PLAIN
[ifdescription] => 0
[ifid] => 0
[lines] => 11
[bytes] => 425
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => UTF-8
)
)
)
[1] => stdClass Object
(
[type] => 0
[encoding] => 4
[ifsubtype] => 1
[subtype] => HTML
[ifdescription] => 0
[ifid] => 0
[lines] => 50
[bytes] => 3875
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => UTF-8
)
)
)
)
)
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 29154
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => what-is-bootstrap.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
)
)
)
Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => 000000000000098145058c769850
)
)
stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 49090
[ifdisposition] => 1
[disposition] => attachment
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => how-ajax-work.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => how-ajax-work.png
)
)
)
应该是:
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 29154
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => what-is-bootstrap.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
)
不过我试过这个:
$body_attachments[$body_number] = array();
if (($structure->parts[$i]->disposition === 'inline')) {
...etc
}
我也试过这个:
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition == 'inline')) {
...etc
}
不会有任何区别。它不会输出 stdClass 的值,其中它有一个名为 inline
.
的值
完整代码如下:
<?php
require_once "Mail.php";
require_once('Mail/IMAPv2.php');
$username = 'username';
$password = 'password';
$mailserver = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX';
$mailbox = imap_open($mailserver, $username, $password) or die("Can't connect: " . imap_last_error());
$key = "mykey";
$email_number = openssl_decrypt(hex2bin($_GET['id']),'AES-128-CBC', $key);
$structure = imap_fetchstructure($mailbox, $email_number);
$body_attachments = array();
$body_number = 0;
for($i = 0; $i < count($structure->parts); $i++) {
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition === 'inline')) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $object->value;
}
}
}
$body_number++;
}
?>
我想做的是我想从 stdClass 数组中搜索值 inline
,然后我想获取值 what-is-bootstrap.png
并将其存储在 $body_attachment
数组。
当我想搜索 inline'
以获取 what-is-bootstrap.png
的值并存储在 [=18 中时,你能告诉我如何从 stdClass 数组中获取值吗? =]数组?
谢谢。
编辑:这是var_dump输出结果:
array(2) {
[0]=> object(stdClass)#4 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(7) "RELATED"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#5 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19c05879c2d0a"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#6 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(11) "ALTERNATIVE"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#7 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19b05879c2d09"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#8 (12) {
["type"]=> int(0)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(5) "PLAIN"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(11)
["bytes"]=> int(425)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#9 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
[1]=> object(stdClass)#10 (12) {
["type"]=> int(0)
["encoding"]=> int(4)
["ifsubtype"]=> int(1)
["subtype"]=> string(4) "HTML"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(48)
["bytes"]=> int(3734)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#11 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
}
} [1]=> object(stdClass)#12 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1)
["id"]=> string(14) ""
["bytes"]=> int(29154)
["ifdisposition"]=> int(1)
["disposition"]=> string(6) "inline"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#13 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(21) "what-is-bootstrap.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#14 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(21) "what-is-bootstrap.png"
} }
}
}
} [1]=> object(stdClass)#15 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1) ["id"]=> string(13) ""
["bytes"]=> int(49090)
["ifdisposition"]=> int(1)
["disposition"]=> string(10) "attachment"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#16 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(17) "how-ajax-work.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#17 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(17) "how-ajax-work.png"
} }
}
}
array(2) {
[0]=> object(stdClass)#4 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(7) "RELATED"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#5 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19c05879c2d0a"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#6 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(11) "ALTERNATIVE"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#7 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19b05879c2d09"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#8 (12) {
["type"]=> int(0)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(5) "PLAIN"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(11)
["bytes"]=> int(425)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#9 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
[1]=> object(stdClass)#10 (12) {
["type"]=> int(0)
["encoding"]=> int(4)
["ifsubtype"]=> int(1)
["subtype"]=> string(4) "HTML"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(48)
["bytes"]=> int(3734)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#11 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
}
}
[1]=> object(stdClass)#12 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1)
["id"]=> string(14) ""
["bytes"]=> int(29154)
["ifdisposition"]=> int(1)
["disposition"]=> string(6) "inline"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#13 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(21) "what-is-bootstrap.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#14 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(21) "what-is-bootstrap.png"
} }
}
}
}
[1]=> object(stdClass)#15 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1)
["id"]=> string(13) ""
["bytes"]=> int(49090)
["ifdisposition"]=> int(1)
["disposition"]=> string(10) "attachment"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#16 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(17) "how-ajax-work.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#17 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(17) "how-ajax-work.png"
} }
}
}
documentation中的用户贡献显示值为INLINE
,而不是inline
。建议进行 case-insensitive 比较。
if ($structure->parts[$i]->ifdisposition && strtolower($structure->parts[$i]->disposition) == 'inline') {
使用 json_decode(json_encode(), true) 将 stdclass 对象简化为关联数组,因为有时在单个变量中处理多个混合对象和数组会很复杂。
我只是把条件和循环语句这部分的代码拿来做个例子
if (($structure->parts[$i]->ifdisposition) && (strtolower($structure->parts[$i]->disposition) === 'inline'))
{
Here is the only part I converted using json_decode(json_encode( $stdclass ),true)
It converts the stdclass object into associative arrays
$parameters = json_decode(json_encode($structure->parts[$i]->parameters), true);
for($i =0; $i < count($parameters); $i++)
{
if(strtolower($parameters[$i]['attribute']) === 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $parameters[$i]['name'];
}
}
循环结束
}
如果要将整个对象转换为关联数组,可以将循环语句从对象支持更改为数组
你也可以使用这个函数 get_object_vars( ) 它也将对象转换为数组,但它们与 json_decode 在深度方面不同我都试过了,
get_object_vars( ) 仅转换父对象而不转换子对象值
$parameters = (object) array('0' =>
(object) array("attribute"=> "name", "value"=>"what-is-bootstrap.png" )
);
print_r($parameters);
print_r(json_decode(json_encode($parameters), true));
print_r(get_object_vars($parameters));
结果是
stdClass Object
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
Array
(
[0] => Array
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
我正在 php 中搜索 stdClass 数组中的值。我很难从数组中获取值,因为当我尝试使用 ifdisposition
和 disposition == 'inline'
搜索值时,它不会获取值并存储在 $body_attachment
数组中我要。
当我尝试这个时:
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition == 'inline')) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $object->value;
}
}
$body_number++;
}
它将输出如下:
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => RELATED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 29154
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => what-is-bootstrap.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
)
)
)
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => RELATED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => 000000000000098145058c769850
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => ALTERNATIVE
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => 000000000000098144058c76984f
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 0
[ifsubtype] => 1
[subtype] => PLAIN
[ifdescription] => 0
[ifid] => 0
[lines] => 11
[bytes] => 425
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => UTF-8
)
)
)
[1] => stdClass Object
(
[type] => 0
[encoding] => 4
[ifsubtype] => 1
[subtype] => HTML
[ifdescription] => 0
[ifid] => 0
[lines] => 50
[bytes] => 3875
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => UTF-8
)
)
)
)
)
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 29154
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => what-is-bootstrap.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
)
)
)
Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => 000000000000098145058c769850
)
)
stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 49090
[ifdisposition] => 1
[disposition] => attachment
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => how-ajax-work.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => how-ajax-work.png
)
)
)
应该是:
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => PNG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 29154
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => what-is-bootstrap.png
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
)
不过我试过这个:
$body_attachments[$body_number] = array();
if (($structure->parts[$i]->disposition === 'inline')) {
...etc
}
我也试过这个:
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition == 'inline')) {
...etc
}
不会有任何区别。它不会输出 stdClass 的值,其中它有一个名为 inline
.
完整代码如下:
<?php
require_once "Mail.php";
require_once('Mail/IMAPv2.php');
$username = 'username';
$password = 'password';
$mailserver = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX';
$mailbox = imap_open($mailserver, $username, $password) or die("Can't connect: " . imap_last_error());
$key = "mykey";
$email_number = openssl_decrypt(hex2bin($_GET['id']),'AES-128-CBC', $key);
$structure = imap_fetchstructure($mailbox, $email_number);
$body_attachments = array();
$body_number = 0;
for($i = 0; $i < count($structure->parts); $i++) {
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition === 'inline')) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $object->value;
}
}
}
$body_number++;
}
?>
我想做的是我想从 stdClass 数组中搜索值 inline
,然后我想获取值 what-is-bootstrap.png
并将其存储在 $body_attachment
数组。
当我想搜索 inline'
以获取 what-is-bootstrap.png
的值并存储在 [=18 中时,你能告诉我如何从 stdClass 数组中获取值吗? =]数组?
谢谢。
编辑:这是var_dump输出结果:
array(2) {
[0]=> object(stdClass)#4 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(7) "RELATED"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#5 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19c05879c2d0a"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#6 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(11) "ALTERNATIVE"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#7 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19b05879c2d09"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#8 (12) {
["type"]=> int(0)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(5) "PLAIN"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(11)
["bytes"]=> int(425)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#9 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
[1]=> object(stdClass)#10 (12) {
["type"]=> int(0)
["encoding"]=> int(4)
["ifsubtype"]=> int(1)
["subtype"]=> string(4) "HTML"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(48)
["bytes"]=> int(3734)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#11 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
}
} [1]=> object(stdClass)#12 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1)
["id"]=> string(14) ""
["bytes"]=> int(29154)
["ifdisposition"]=> int(1)
["disposition"]=> string(6) "inline"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#13 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(21) "what-is-bootstrap.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#14 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(21) "what-is-bootstrap.png"
} }
}
}
} [1]=> object(stdClass)#15 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1) ["id"]=> string(13) ""
["bytes"]=> int(49090)
["ifdisposition"]=> int(1)
["disposition"]=> string(10) "attachment"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#16 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(17) "how-ajax-work.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#17 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(17) "how-ajax-work.png"
} }
}
}
array(2) {
[0]=> object(stdClass)#4 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(7) "RELATED"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#5 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19c05879c2d0a"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#6 (11) {
["type"]=> int(1)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(11) "ALTERNATIVE"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#7 (2) {
["attribute"]=> string(8) "boundary"
["value"]=> string(28) "0000000000004ee19b05879c2d09"
} }
["parts"]=> array(2) {
[0]=> object(stdClass)#8 (12) {
["type"]=> int(0)
["encoding"]=> int(0)
["ifsubtype"]=> int(1)
["subtype"]=> string(5) "PLAIN"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(11)
["bytes"]=> int(425)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#9 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
[1]=> object(stdClass)#10 (12) {
["type"]=> int(0)
["encoding"]=> int(4)
["ifsubtype"]=> int(1)
["subtype"]=> string(4) "HTML"
["ifdescription"]=> int(0)
["ifid"]=> int(0)
["lines"]=> int(48)
["bytes"]=> int(3734)
["ifdisposition"]=> int(0)
["ifdparameters"]=> int(0)
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#11 (2) {
["attribute"]=> string(7) "charset"
["value"]=> string(5) "UTF-8"
} }
}
}
}
[1]=> object(stdClass)#12 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1)
["id"]=> string(14) ""
["bytes"]=> int(29154)
["ifdisposition"]=> int(1)
["disposition"]=> string(6) "inline"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#13 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(21) "what-is-bootstrap.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#14 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(21) "what-is-bootstrap.png"
} }
}
}
}
[1]=> object(stdClass)#15 (14) {
["type"]=> int(5)
["encoding"]=> int(3)
["ifsubtype"]=> int(1)
["subtype"]=> string(3) "PNG"
["ifdescription"]=> int(0)
["ifid"]=> int(1)
["id"]=> string(13) ""
["bytes"]=> int(49090)
["ifdisposition"]=> int(1)
["disposition"]=> string(10) "attachment"
["ifdparameters"]=> int(1)
["dparameters"]=> array(1) {
[0]=> object(stdClass)#16 (2) {
["attribute"]=> string(8) "filename"
["value"]=> string(17) "how-ajax-work.png"
} }
["ifparameters"]=> int(1)
["parameters"]=> array(1) {
[0]=> object(stdClass)#17 (2) {
["attribute"]=> string(4) "name"
["value"]=> string(17) "how-ajax-work.png"
} }
}
}
documentation中的用户贡献显示值为INLINE
,而不是inline
。建议进行 case-insensitive 比较。
if ($structure->parts[$i]->ifdisposition && strtolower($structure->parts[$i]->disposition) == 'inline') {
使用 json_decode(json_encode(), true) 将 stdclass 对象简化为关联数组,因为有时在单个变量中处理多个混合对象和数组会很复杂。
我只是把条件和循环语句这部分的代码拿来做个例子
if (($structure->parts[$i]->ifdisposition) && (strtolower($structure->parts[$i]->disposition) === 'inline'))
{
Here is the only part I converted using json_decode(json_encode( $stdclass ),true)
It converts the stdclass object into associative arrays
$parameters = json_decode(json_encode($structure->parts[$i]->parameters), true);
for($i =0; $i < count($parameters); $i++)
{
if(strtolower($parameters[$i]['attribute']) === 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $parameters[$i]['name'];
}
}
循环结束
}
如果要将整个对象转换为关联数组,可以将循环语句从对象支持更改为数组
你也可以使用这个函数 get_object_vars( ) 它也将对象转换为数组,但它们与 json_decode 在深度方面不同我都试过了, get_object_vars( ) 仅转换父对象而不转换子对象值
$parameters = (object) array('0' =>
(object) array("attribute"=> "name", "value"=>"what-is-bootstrap.png" )
);
print_r($parameters);
print_r(json_decode(json_encode($parameters), true));
print_r(get_object_vars($parameters));
结果是
stdClass Object
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
Array
(
[0] => Array
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)