我正在处理我的 php 以获取数据以将它们存储在数组中。我对数组中的索引值有问题,因为索引值将从 1 开始,然后将其计数到 2、3、4...等,它应该从 0 开始,然后是 1、2 3.. .etc,因为我使用$i = 0;默认从零开始。
这是我使用的索引值以 1 开头的内容:
if(isset($structure->parts) && count($structure->parts)) {
for($i = 0; $i < count($structure->parts); $i++) {
if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition == 'attachment')) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
$attachments[$i]['attachment'] = '';
}
}
}
}
我已经从试图改变$i++来$i和我试图把$i++在for循环,但没有奏效。
输出:
Array ( [1] => Array ( [is_attachment] => 1 [name] => 2019-01-23 (1).rar [attachment] => ) [2] => Array ( [is_attachment] => 1 [name] => email.zip [attachment] => ) )
它应该是:
Array ( [0] => Array ( [is_attachment] => 1 [name] => 2019-01-23 (1).rar [attachment] => ) [1] => Array ( [is_attachment] => 1 [name] => email.zip [attachment] => ) )
这是完整的代码:
<?php
require_once "Mail.php";
require_once('Mail/IMAPv2.php');
$username = 'username';
$password = 'password';
$mailserver = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX';
$mailbox = imap_open($mailserver, $username, $password) or die("Can't connect: " . imap_last_error());
$key = "key";
$email_number = openssl_decrypt(hex2bin('477'),'AES-128-CBC', $key);
$attach_id = $_GET['attid'];
/* get information specific to this email */
$overview = imap_fetch_overview($mailbox, $email_number, 0);
$message = imap_fetchbody($mailbox, $email_number, 2);
/* get mail structure */
$structure = imap_fetchstructure($mailbox, $email_number);
$attachments = array();
$attachment_number = 0;
我无法找出为什么索引值总是从 1 开始,而它应该从 0 开始,然后是 1、2、3,因为它每次都在计算值。
你能告诉我一个例子,当我使用时,我如何以 0 作为默认值开始索引值,然后将它计数到 1,然后是 2、3、4、5...等$i++?
谢谢你。
哔哔one
MYYA
跃然一笑