如何将PHP中数组的索引值设置为0

  • 本文关键字:索引值 设置 数组 PHP php
  • 更新时间 :
  • 英文 :


我正在研究我的PHP,以获取数据将它们存储在数组中。我对数组中的索引值有问题,因为索引值将以1开头,然后将其计算为2、3、4 ...等,它应该以0为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;  
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'] = '';
                }
            }
        }
    }
    ?>

我无法找出为什么索引值始终以1的速度开始时,当它以零开始,然后是1、2、3,因为它每次都会计算值。

您能为我展示一个示例,我如何以0为默认值启动索引值,然后在使用$i++时最多将其计算为1,2、3、4、5 ...等。

谢谢。

这是因为在所有情况下$structure->parts[0]都不匹配$structure->parts[$i]->disposition == 'attachment'

仅当一个正确时,仅在数组中创建一个新项目,并且不使用循环计数器使用simpe $arr[]来创建下一个事件

$attachments = array();
if(isset($structure->parts) && count($structure->parts)) {
    foreach ($structure->parts as $part) {
        if (($part->ifdisposition) && ($part->disposition == 'attachment')) {
            foreach($part->parameters as $obj) {
                if(strtolower($obj->attribute) == 'name') {
                    $t['is_attachment'] = true;
                    $t['name'] = $obj->value;
                    $t['attachment'] = '';
                    $attachments[] = $t
                }
            }
        }
    }
}

尝试此

foreach($structure->parts[$i]->parameters as $object) {
            if(strtolower($object->attribute) == 'name') {
                $attachment['is_attachment'] = true;
                $attachment['name'] = $object->value;
                $attachment['attachment'] = '';
                $attachments[]=$attachment;
            }
        }

如果您想知道为什么数组的索引以1而不是0开头,请向我们展示数组的定义。但是,您也可以通过使用PHP Array_keys函数获取数组键

,带有未知键的循环数组
$this->arr = [1 => 'one', 2 => 'two', 3 => 'three'];
    $keys = array_keys($this->arr);
    for($i = 0; $i < count($keys); $i++) {
        $value = $this->arr[$keys[$i]];
        echo 'val: ' .$value.'<br>';
    }

或者您可以将两个foreach包裹到另一个

foreach($structure->parts as $key => $part){
        $part->ifdisposition = true;
        $part->disposition = 'attachment';
        if (($part->ifdisposition) && ($part->disposition == 'attachment')) {
            foreach($part->parameters as $object) {
                if(strtolower($object->attribute) == 'name') {
                    $attachments[$key]['is_attachment'] = true;
                    $attachments[$key]['name'] = $object->value;
                    $attachments[$key]['attachment'] = '';
                }
            }
        }
    }

另一个选项是使用array_map重新键键,但是您的数组将被更改,因此,如果需要原始数组,则可以将其缓存到另一个变量中。

在这里:

$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
$attachments[$i]['attachment'] = '';

您将键设置为$i,因此$structure->parts中的第一个元素匹配标准是循环中的第二个元素。要将$attachments数组从零开始,您只需要让PHP创建键本身:

$attachments[] = ['is_attachment' => true, 'name' => $object->value, 'attachment' => ''];

最新更新