Welcome Guest, Not a member yet? Register   Sign In
Looping multidimensional array just show one index while index is more than one
#1

Hi,
I've make a function to get messages and store it into an array and get attachments from each messages then make array for it also if attachment was exist.
in this case there is a message that has two attachment but when i looping and use dd(); it just show one attachment only.
here is my code:

Code:
    public function getTlMessages(){
        $mailbox = $this->getTlMailBox();
        $messages = $mailbox->getMessages();
        $messagelist = [];;
        $counter = 0;
        foreach ($messages as $message) {
            $counter++;
            $attachment = $message->getAttachments();
            $msg['id'] = $counter;
            $msg['gmailid'] = $message->getId();
            $msg['number'] = $message->getNumber();
            $msg['subject'] = $message->getSubject();
            $msg['attachment'] = array();
            foreach ($attachment as $attach){
                $attachlist['name'] = $attach->getFilename();
                $attachlist['subtype'] = $attach->getSubtype();
                $msg['attachment'] = $attachlist;
            }
            $messagelist[] = $msg;
        }
        dd($messagelist);
        return $messages;
    }


Here is the result:
it supposed to be two pdf file instead of one in "attachment"
[Image: Screen-Shot-2021-12-01-at-00-14-00.png]
Reply
#2

(This post was last modified: 11-30-2021, 10:33 PM by iRedds.)

You re-declare the value each time.
PHP Code:
$msg['attachment'] = $attachlist;

//use 
$msg['attachment'][] = $attachlist;
//or 
$msg['attachment'][] = [
    
'name' => ....,
    
'subtype' => ....,
]; 
Reply
#3

(11-30-2021, 07:53 PM)iRedds Wrote: You re-declare the value each time.
PHP Code:
$msg['attachment'] = $attachlist;

//use 
$msg['attachment'][] = $attachlist;
//or 
$msg['attachment'][] = [
    'name' => ....,
    'subtype' => ....,
]; 

Thankyouu, it's work for me
Reply




Theme © iAndrew 2016 - Forum software by © MyBB