如何从数组中获取元素?

时间:2019-07-03 18:50:34

标签: php

我正在使用php来搜索数组中的元素。我很难从数组中获取所需的数据,因为它不会获取ifdispositiondisposition == 'inline'之间的数据。

当我尝试这样做时:

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 
    [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 
                                       ) 
                                   ) 
                ) 
             ) 
) 



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 
                      ) 
                  ) 
 )

这是完整的代码:

<?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++;
 }
 ?>

您能给我一个例子,当我想搜索ifdispositiondisposition == 'inline'时如何从数组中获取元素吗?

谢谢。

编辑:这是我已打印输出的var_export:

stdClass::__set_state(array( 
    'type' => 1, 
    'encoding' => 0, 
    'ifsubtype' => 1, 
    'subtype' => 'RELATED', 
    'ifdescription' => 0, 
    'ifid' => 0, 
    'ifdisposition' => 0, 
    'ifdparameters' => 0, 
    'ifparameters' => 1, 
    'parameters' => array ( 
                        0 => stdClass::__set_state(array( 
                                'attribute' => 'boundary', 
                                'value' => '0000000000004ee19c05879c2d0a', 
                            )), ), 
    'parts' => array ( 
                 0 => stdClass::__set_state(array( 
                          'type' => 1, 
                          'encoding' => 0, 
                          'ifsubtype' => 1, 
                          'subtype' => 'ALTERNATIVE', 
                          'ifdescription' => 0, 
                          'ifid' => 0, 
                          'ifdisposition' => 0, 
                          'ifdparameters' => 0, 
                          'ifparameters' => 1, 
                          'parameters' => array ( 
                                            0 => stdClass::__set_state(array( 
                                                    'attribute' => 'boundary', 
                                                    'value' => '0000000000004ee19b05879c2d09',
                                                )), ), 
                          'parts' => array ( 
                                        0 => stdClass::__set_state(array( 
                                                 '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::__set_state(array( 
                                                                             'attribute' => 'charset', 
                                                                             'value' => 'UTF-8', 
                                                                        )), 
                                                                    ), 
                                                 )), 
                                        1 => stdClass::__set_state(array( 
                                                 'type' => 0, 
                                                 'encoding' => 4, 
                                                 'ifsubtype' => 1, 
                                                 'subtype' => 'HTML', 
                                                 'ifdescription' => 0, 
                                                 'ifid' => 0, 
                                                 'lines' => 48, 
                                                 'bytes' => 3734, 
                                                 'ifdisposition' => 0, 
                                                 'ifdparameters' => 0, 
                                                 'ifparameters' => 1, 
                                                 'parameters' => array ( 
                                                                   0 => stdClass::__set_state(array( 
                                                                            'attribute' => 'charset', 
                                                                            'value' => 'UTF-8', 
                                                                        )), ), 
                                                 )), 
                                    ), 
                         )), 
                 1 => stdClass::__set_state(array( 
                           '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::__set_state(array( 
                                                        'attribute' => 'filename', 
                                                        'value' => 'what-is-bootstrap.png', 
                                                    )), 
                                                ), 
                           'ifparameters' => 1, 
                           'parameters' => array ( 
                                             0 => stdClass::__set_state(array( 
                                                      'attribute' => 'name', 
                                                      'value' => 'what-is-bootstrap.png', 
                                                    )), 
                                                ), 
                            )), 
    ), 
))

stdClass::__set_state(array( 
    '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::__set_state(array( 
                                'attribute' => 'filename', 
                                'value' => 'how-ajax-work.png', 
                            )), 
                        ), 
   'ifparameters' => 1, 
   'parameters' => array ( 
                     0 => stdClass::__set_state(array( 
                              'attribute' => 'name', 
                              'value' => 'how-ajax-work.png', 
                            )), 
                        ), 
))

0 个答案:

没有答案