Jump to content

Weird array problem


mangy1983

Recommended Posts

I have an array that displays the following information with the print_r function:

 

Array

(

[0] => Array

(

[0] => 2206.235

[1] => 3.9

[2] => 0.48

[3] => 0.477

[4] => 0.477

[5] => 0.475

 

 

[215] => 0.434

[216] => 0.436

[217] => 0.435

[218] => 0.434

[219] => 0.435

[220] => 0.435

)

)

Array

(

[0] => Array

(

[0] => 2206.235

[1] => 3.9

[2] => 0.48

[3] => 0.48

[4] => 0.435

[5] => 0.435

)

)

 

I would like to save the last row to my database which in this case is 220 but the last row could vary from time to time as the array is created from an email attachment so l wanted a code that would find the number of rows in the array.

 

When l echo out the row like this: echo $matches[0][210] it retrieves the correct information so l wanted to implement this code instead to cater for an unknown amount of array rows so that the highest row was always returned:

 

$i = count($matches[0]);

 

echo $matches[0][$i];

 

but this does not work as when echoing $i the result is 2216 instead of 220 which l find odd or l am doing something wrong. Any ideas?

 

thanks Callum

 

Link to comment
Share on other sites

Try end.

 

Kindoff works mate but it is displaying two bits of information. I copied pasted the array again this time in code tags as l noted that when l didn't the 0' were removed. Its kind off weird but l think there are 2 array 0's

 

thanks Callum

 

Array 
(
[0] => Array 
(
[0] => 2206.235 
[1] => 3.9 
[2] => 0.48 
[3] => 0.477 
[4] => 0.477 
[5] => 0.475 


[215] => 0.434 
[216] => 0.436 
[217] => 0.435 
[218] => 0.434 
[219] => 0.435 
[220] => 0.435 
) 
) 
Array 
(
[0] => Array 
(
[0] => 2206.235 
[1] => 3.9 
[2] => 0.48 
[3] => 0.48 
[4] => 0.435 
[5] => 0.435 
) 
)

Link to comment
Share on other sites

I don't see a value of 2216 anywhere in there. Where would that be coming from?

 

I honestly don't know unless it is something to do with the fact that for some reason l think l have two 0 arrays. I have removed the middle rows of the array l posted to save space as they are pretty much the same information.

 

cheers Callum

Link to comment
Share on other sites

I know what is wrong now but cant fathom what is causing my problem. There are numerous loops. When $matches is print_r'd in its current loop it shows the first array 0 (the correct one) and the second array 0 but when l move it to any of the other loops it only displays the second array 0. Any ideas guys. If anyone wants to have a go l can give them the correct details for the censored out areas at the top of the by pm

 

thanks Callum

 

Below is my whole code bar my personal details:

 

<?php

           
    $mbox = imap_open("{mail.*****.co.uk:143/notls}INBOX", "creedlevel@****.co.uk", "******");  //connects to mailbox on your server
     
     
    $headers = imap_headers($mbox); 
   
    if ($headers == false) { 
        print_r(imap_errors()); 
    } else {





$j = imap_num_msg($mbox);

//if there is a message in your inbox
if( $j > 0 ) {         //this just reads the most recent email. In order to go through all the emails, you'll have to loop through the number of messages








            $info = imap_headerinfo($mbox,$j); 

             
            




            $structure = imap_fetchstructure($mbox, $info->Msgno); 
            
            $attachments = array(); 
             
            if (isset($structure->parts) && count($structure->parts)) { 
                for ($i = 0; $i < count($structure->parts); $i++) { 
                    $attachments[$i] = array( 
                        'is_attachment' => false, 
                        'filename' => '', 
                        'name' => '', 
                        'attachment' => '' 
                    ); 
                     
                    if($structure->parts[$i]->ifdparameters) { 
                        foreach($structure->parts[$i]->dparameters as $object) { 
                            if(strtolower($object->attribute) == 'filename') { 
                                $attachments[$i]['is_attachment'] = true; 
                                $attachments[$i]['filename'] = $object->value; 
                            } 
                        } 
                    } 
                     
                    if($structure->parts[$i]->ifparameters) { 
                        foreach($structure->parts[$i]->parameters as $object) { 
                            if(strtolower($object->attribute) == 'name') { 
                                $attachments[$i]['is_attachment'] = true; 
                                $attachments[$i]['name'] = $object->value; 
                            } 
                        } 
                    } 
                     
                    if($attachments[$i]['is_attachment']) { 
                        $attachments[$i]['attachment'] = imap_fetchbody( 
                            $mbox, $info->Msgno, $i+1); 
                        if($structure->parts[$i]->encoding == 3) { 
                            // 3 = BASE64 
                            $attachments[$i]['attachment'] = 
                                base64_decode($attachments[$i]['attachment']); 
                        } elseif($structure->parts[$i]->encoding == 4) { 
                            // 4 = QUOTED-PRINTABLE 
                            $attachments[$i]['attachment'] =  
                                quoted_printable_decode($attachments[$i]['attachment']); 
                        } 
                    $attachment =  $attachments[$i]['attachment']; 

                    
preg_match_all("#\d+\.\d+#", $attachment, $matches);


print_r ($matches);









   


                    }














                } 






            }


                     
        } 



    } 
     


imap_close($mbox); 
    
     
?>

Link to comment
Share on other sites

This code:

$i = count($matches[0]);

echo $matches[0][$i];

 

Will NOT work, as $i would be 221 (which leads me to believe that you echo'd the $i and then later a 6 to get the returned number.

 

The reason it would be 221 is that array's start at 0, but count starts at 1.  So you need to subtract 1 from the count, in order to get the highest key from a numerical array.

Link to comment
Share on other sites

sorry guys l figured it out. It was just the fact that l was echoing out the variables whilst the variable providing the information was still in a loop.  I used the end array instead of what l was trying to do. I was aware of the array data starting at 0 and that l would have to subtract one form it but the problem was that 2 array 0's were being echoed out which was causing the weird results. I changed this line: $attachment =  $attachments[$i]['attachment']; by removing the $i and hard coding the number 1 in its place as the original code l used before modifying it to my own use loaded up all emails and then loaded up all attachments whereas l only wanted the last emails attachment loaded up.

 

thanks Callum

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.