dtcyv3985 2015-11-12 18:13
浏览 57

PHP:将自定义密钥对推送到数组中

I am trying to get dates from emails using imap with PHP. I've got my messages from my inbox and sent items, then I've merged them together, but now I need to put them into order by date sent.

$emailFROM = imap_search($mbox, 'FROM "'.$address.'"', SE_UID);
$emailTO = imap_search($obox, 'TO "'.$address.'"', SE_UID);
$thread = array_merge($emailFROM, $emailTO);

So that's the array of all messages. It outputs like this:

Array
(
    [0] => 445
    [1] => 446
    [2] => 450
    [3] => 462
    [4] => 468
    [5] => 110
    [6] => 112
    [7] => 114
    [8] => 167
    [9] => 169
    [10] => 170
)

Unfortunately, the emailFROM numbers and the emailTO numbers do not line up (emailFROM are numbers 445-468, emailTO is 110-170)

So now I need to go through each email and find the date, then I want to push the date into an array using the message ID number as the key

This is what I have so far:

$emailFROM = imap_search($mbox, 'FROM "'.$address.'"', SE_UID);
$emailTO = imap_search($obox, 'TO "'.$address.'"', SE_UID);
$thread = array_merge($emailFROM, $emailTO);
$full_thread = array();
foreach($thread as $single){
    $header = imap_header($mbox, $single);
    $date = $header->date;
    $full_thread[$from]=$date;
}
//Then I will arrange the array when the array works
print_r($full_thread);

But I believe this doesn't work because it's looking for the $from id in the array first? So, I'm not sure where to go from here.

Thanks!

  • 写回答

1条回答 默认 最新

  • duanjieyi6582 2015-11-12 18:23
    关注

    It would seem more poignant to define the array keys instead of performing a blind array_merge(). In this way, you can access and update the index on full_thread based on the index of the array item, for example:

    $thread['from'] = $emailFROM;
    $thread['to']   = $emailTO;
    

    Now your foreach loop can keep a reference to the index.

    foreach($thread as $type => $single){
    

    Then you can change your array key definition on full_thread to your $type index.

        $full_thread[$type]['date'] = $date;
    

    The extra ['date'] key is not required.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线