亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在php中將數組的索引值設置為0?

如何在php中將數組的索引值設置為0?

PHP
翻閱古今 2021-09-18 13:44:11
我正在處理我的 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] => ) )這是完整的代碼:<?phprequire_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++?謝謝你。
查看完整描述

3 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

這是因為在所有情況下$structure->parts[0]都不匹配$structure->parts[$i]->disposition == 'attachment'。


僅在正確時才在數組中創建新項目,并且不要使用循環計數器使用簡單$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

                }

            }

        }

    }

}


查看完整回答
反對 回復 2021-09-18
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

如果您想知道為什么數組的索引以 1 開頭而不是 0,請向我們展示數組的定義。但是您也可以通過使用 PHP array_keys函數獲取數組鍵來使用 for 循環循環使用未知鍵的數組


$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>';

    }

或者你可以將兩個 foreaches 包裝成另一個


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重新映射數組的鍵,但您的數組將被更改,因此如果您需要原始數組,您可以將其緩存到另一個變量中。


查看完整回答
反對 回復 2021-09-18
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

這里:


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



查看完整回答
反對 回復 2021-09-18
  • 3 回答
  • 0 關注
  • 252 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號