CodeIgniter Forums
Looping multidimensional array just show one index while index is more than one - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Looping multidimensional array just show one index while index is more than one (/showthread.php?tid=80676)



Looping multidimensional array just show one index while index is more than one - zhikri - 11-30-2021

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]


RE: Looping multidimensional array just show one index while index is more than one - iRedds - 11-30-2021

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

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



RE: Looping multidimensional array just show one index while index is more than one - zhikri - 12-01-2021

(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