如何在php中将数组的索引值设置为0?

我正在处理我的 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++?


谢谢你。


翻阅古今
浏览 224回答 3
3回答

哔哔one

这是因为在所有情况下$structure->parts[0]都不匹配$structure->parts[$i]->disposition == 'attachment'。仅在正确时才在数组中创建新项目,并且不要使用循环计数器使用简单$arr[]构造来创建下一次出现$attachments = array();if(isset($structure->parts) && count($structure->parts)) {&nbsp; &nbsp; foreach ($structure->parts as $part) {&nbsp; &nbsp; &nbsp; &nbsp; if (($part->ifdisposition) && ($part->disposition == 'attachment')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($part->parameters as $obj) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(strtolower($obj->attribute) == 'name') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $t['is_attachment'] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $t['name'] = $obj->value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $t['attachment'] = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attachments[] = $t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

MYYA

如果您想知道为什么数组的索引以 1 开头而不是 0,请向我们展示数组的定义。但是您也可以通过使用 PHP array_keys函数获取数组键来使用 for 循环循环使用未知键的数组$this->arr = [1 => 'one', 2 => 'two', 3 => 'three'];&nbsp; &nbsp; $keys = array_keys($this->arr);&nbsp; &nbsp; for($i = 0; $i < count($keys); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $value = $this->arr[$keys[$i]];&nbsp; &nbsp; &nbsp; &nbsp; echo 'val: ' .$value.'<br>';&nbsp; &nbsp; }或者你可以将两个 foreaches 包装成另一个foreach($structure->parts as $key => $part){&nbsp; &nbsp; &nbsp; &nbsp; $part->ifdisposition = true;&nbsp; &nbsp; &nbsp; &nbsp; $part->disposition = 'attachment';&nbsp; &nbsp; &nbsp; &nbsp; if (($part->ifdisposition) && ($part->disposition == 'attachment')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($part->parameters as $object) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(strtolower($object->attribute) == 'name') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attachments[$key]['is_attachment'] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attachments[$key]['name'] = $object->value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attachments[$key]['attachment'] = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }另一种选择是使用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' => ''];
打开App,查看更多内容
随时随地看视频慕课网APP