PHP tworzy wielowymiarową tablicę wątków wiadomości z wielowymiarowej tablicy (IMAP)

Moje pytanie brzmi:

Jeśli spojrzysz poniżej, zobaczysz strukturę danych z identyfikatorami wiadomości, a następnie końcową strukturę danych zawierającą szczegóły wiadomości, które powinny być zagregowane zimap_fetch_overview. Identyfikatory wiadomości pochodzą zimap_thread. Problem polega na tym, że nie umieszcza danych e-mail w pozycji, w której znajduje się identyfikator wiadomości.

Oto moja struktura danych:

[5] => Array
    (
        [0] => 5
        [1] => 9
    )

[10] => Array
    (
        [0] => 10
        [1] => 11
    )

Chciałbym mieć:

[5] => Array
    (
        [0] => messageDetails for id 5
        [1] => messageDetails for id 9
    )

[10] => Array
    (
        [0] => messageDetails for id 10
        [1] => messageDetails for id 11
    )

Oto kod, który mam do tej pory:

$emails = imap_fetch_overview($imap, implode(',',$ids));

// root is the array index position of the threads message, such as 5 or 10
foreach($threads as $root => $messages){

    // id is the id being given to us from `imap_thread`
    foreach($message as $key => $id){

      foreach($emails as $index => $email){

         if($id === $email->msgno){
             $threads[$root][$key] = $email;
             break;
          }
      }
    }
 }

Oto wydruk z jednego z e-maili $:

    [0] => stdClass Object
    (
        [subject] => Cloud Storage Dump
        [from] => Josh Doe
        [to] => [email protected]
        [date] => Mon, 21 Jan 2013 23:18:00 -0500
        [message_id] => <[email protected]>
        [size] => 2559
        [uid] => 5
        [msgno] => 5
        [recent] => 0
        [flagged] => 0
        [answered] => 1
        [deleted] => 0
        [seen] => 0
        [draft] => 0
        [udate] => 1358828308
    )

Jeśli zauważysz, msgno to 5, które koliduje z$id, więc technicznie dane powinny wypełniać ostateczną strukturę danych.

Wydaje się to również nieskutecznym sposobem radzenia sobie z tym.

Daj mi znać, jeśli potrzebujesz dodatkowych wyjaśnień.

AKTUALIZUJ KOD

Ten kod jest kombinacją kodu znalezionego na php api i niektórych poprawkach przeze mnie. To, co uważam za problematyczne, to nadal$root.

$addedEmails = array();
$thread = imap_thread($imap);
foreach ($thread as $i => $messageId) { 
    list($sequence, $type) = explode('.', $i); 
    //if type is not num or messageId is 0 or (start of a new thread and no next) or is already set 
   if($type != 'num' || $messageId == 0 || ($root == 0 && $thread[$sequence.'.next'] == 0) || isset($rootValues[$messageId])) { 
    //ignore it 
    continue; 
} 

if(in_array($messageId, $addedEmails)){
    continue;
}
array_push($addedEmails,$messageId);

//if this is the start of a new thread 
if($root == 0) { 
    //set root 
    $root = $messageId; 
} 

//at this point this will be part of a thread 
//let's remember the root for this email 
$rootValues[$messageId] = $root; 

//if there is no next 
if($thread[$sequence.'.next'] == 0) { 
    //reset root 
    $root = 0; 
    } 
  }
$ids=array();
$threads = array();
foreach($rootValues as $id => $root){
    if(!array_key_exists($root,$threads)){
        $threads[$root] = array();
    }
    if(!in_array($id,$threads[$root])){
        $threads[$root][] = $id;
       $ids[]=$id;
    }
 }
 $emails = imap_fetch_overview($imap, implode(',', array_keys($rootValues)));

 $keys = array();
 foreach($emails as $k => $email)
 {
$keys[$email->msgno] = $k;
 }

 $threads = array_map(function($thread) use($emails, $keys)
{
// Iterate emails in these threads
return array_map(function($msgno) use($emails, $keys)
{
    // Swap the msgno with the email details
    return $emails[$keys[$msgno]];

}, $thread);
}, $threads);

questionAnswers(4)

yourAnswerToTheQuestion