douruobokui58233 2019-06-29 18:56 采纳率: 0%
浏览 128
已采纳

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

I am working on my php to fetch the data to store them in an array. I have got a problem with the index value in the array, because the index value will start with 1 then it will count it up to 2, 3, 4...etc which it should start with 0 then 1, 2 3...etc, because I am using $i = 0; to start with zero as default.

Here is what I use that the index value start with 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 have tried to change from $i++ to $i and I tried to put $i++ in the for loop, but it didn't work.

Output:

Array ( [1] => Array ( [is_attachment] => 1 [name] => 2019-01-23 (1).rar [attachment] => ) [2] => Array ( [is_attachment] => 1 [name] => email.zip [attachment] => ) )

It should be:

Array ( [0] => Array ( [is_attachment] => 1 [name] => 2019-01-23 (1).rar [attachment] => ) [1] => Array ( [is_attachment] => 1 [name] => email.zip [attachment] => ) )

Here is the full code:

<?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'] = '';
                }
            }
        }
    }
    ?>

I am unable to find out why the index value have always start with 1 when it should have start with zero then 1, 2, 3 as it get counting up the value each time.

Can you please show me an example how I can start the index value with 0 as a default then count it up to 1, then 2, 3, 4, 5...etc when I am using $i++?

Thank you.

  • 写回答

4条回答 默认 最新

  • duanqiongchong0354 2019-06-29 19:36
    关注

    It's because $structure->parts[0] does not match $structure->parts[$i]->disposition == 'attachment' in all cases.

    Only create a new item in your array when one is correct and dont use the loop counter use a simpe $arr[] contruct to create the next occurance

    $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
                    }
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料