I'm playing with the imap_getmailboxes() function, and it's working great!

This is what I coded:

<?php
// list all mailboxes
$boxes = imap_getmailboxes($imap, "{localhost}", "*");
 foreach($boxes as $val) { 
  $name = $val->name; 
  ?>

<table><tr><td><? echo $name; ?></td></tr></table>

<? } ?>

And this is what is returned:

{localhost}FOLDER 1
{localhost}FOLDER 2
{localhost}FOLDER 3
{localhost}INBOX
{localhost}Sent Items

I want to get rid of the {localhost} part, but it seems to be a part of the array. I played with the explode() function and it worked. But now I'm wondering, is there a way to NOT show any results from an array?

I want to not show INBOX, Sent Items, Trash, and a few others. I plan to put those at the top, while I wish to alphabetize the rest.
Does the foreach() have a way to do that?

Any thoughts?

    Any suitable loop will do. You can remove '{localhost}' from the front of the string with [man]substr_replace/man. After that, it is a matter of checking if the remaining string matches 'INBOX', 'Sent Items', 'Trash', etc, and if so, do not place it into the array.

    Alternatively, you can just dump everything into the array, then use [man]array_diff/man with an array of unwanted items.

      Very helpful! I added another array to take out folders I didn't need.

      Do you know how I would alphabetize the results in the foreach array?

        Pass $boxes through a call to [man]usort[/man] along with the name of a function that compares boxes' name properties.

          Write a Reply...