如何使用 php imap_sort 排序和检索最后 10 条消息
How to use php imap_sort to sort and retrieve last 10 messages
我想从 gmail 帐户中检索最后 10 封邮件并将它们显示在一个页面中。到目前为止我有以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- <!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css/styles.css" rel="stylesheet">
<!--<link rel="stylesheet" href="mail.css"/> -->
</head>
<body>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
/* connect to gmail */
$user = '*****';
$password = '*********';
$mailbox = "{imap.gmail.com:993/imap/ssl}INBOX";
?>
<?php
$mbx = imap_open($mailbox , $user , $password);
/*imap_check returns information about the mailbox
including mailbox name and number of messages*/
//$check = imap_check($mbx);
/* grab emails */
$emails = imap_search($mbx, 'SINCE "10 Mar 2015"');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
?>
<table class="table table-bordered table-hover">
<tr>
<th>From</th>
<th>Date</th>
<th>Subject</th>
</tr>
<?php
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overviews = imap_fetch_overview($mbx,$email_number,0);
/*imap_fetch_overview returns an overview for a message.
An overview contains information such as message subject,
sender, date, and if it has been seen. Note that it does
not contain the body of the message. Setting the second
parameter to "1:n" will cause it to return a sequence of messages*/
//$overviews = imap_fetch_overview($mbx,"1:{$check->Nmsgs}");
?>
<?php
/* put the newest emails on top.Doesnt work.*/
// rsort($overviews);
rsort($overviews);
print_r($overviews);
foreach($overviews as $overview)
{
?>
<tr>
<td><?php echo $overview->from; ?></td>
<td><?php echo $overview->date; ?></td>
<td><a href="open.php?id=<?php echo $overview->uid; ?>"><?php echo $overview->subject; ?></a></td>
</tr>
<?php
}
}
}
?>
</table>
</body>
</html>
我在此处看到 post How can I sort arrays and data in PHP?,但我很难理解如何对 $overviews[date] 进行排序。 rsort 或任何其他类型不起作用。您如何指定要在 [date] property.thanks.
排序
P.S。这是数组:
Array ( [0] => stdClass Object ( [subject] => Fwd: A Short Course STI #4653 [from] => Fran ***8olo [to] => Fran ****lo [date] => Tue, 10 Mar 2015 12:42:46 GMT [message_id] => <-7376330247335926430@unknownmsgid> [size] => 28928 [uid] => 1532 [msgno] => 743 [recent] => 0 [flagged] => 0 [answered] => 0 [deleted] => 0 [seen] => 0 [draft] => 0 [udate] => 1425991366 ) )
使用 usort 更新代码:
<?php
/* grab emails */
$emails = imap_search($mbx, 'SINCE "10 Mar 2015"');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
?>
<table class="table table-bordered table-hover">
<tr>
<th>From</th>
<th>Date</th>
<th>Subject</th>
</tr>
<?php
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overviews = imap_fetch_overview($mbx,$email_number,0);
/*imap_fetch_overview returns an overview for a message.
An overview contains information such as message subject,
sender, date, and if it has been seen. Note that it does
not contain the body of the message. Setting the second
parameter to "1:n" will cause it to return a sequence of messages*/
//$overviews = imap_fetch_overview($mbx,"1:{$check->Nmsgs}");
?>
<?php
/* put the newest emails on top.Doesnt work.*/
// rsort($overviews);
usort($overviews, function($a1, $a2) {
$v1 = strtotime($a1['date']);
$v2 = strtotime($a2['date']);
return $v1 - $v2; // $v2 - $v1 to reverse direction
});
print_r($overviews);
foreach($overviews as $overview)
{
?>
<tr>
<td><?php echo $overview->from; ?></td>
<td><?php echo $overview->date; ?></td>
<td><a href="open.php?id=<?php echo $overview->uid; ?>"><?php echo $overview->subject; ?></a></td>
</tr>
<?php
}
}
}
?>
</table>
当我检索电子邮件时仍然随机排序。
您可以在 php
中使用 USORT
usort($overviews, function($a1, $a2) {
$v1 = strtotime($a1['date']);
$v2 = strtotime($a2['date']);
return $v1 - $v2; // $v2 - $v1 to reverse direction
});
我修复了它,只需使用 Php 内置的 rsort() 函数即可。
<?php rsort($emails); ?>
我想从 gmail 帐户中检索最后 10 封邮件并将它们显示在一个页面中。到目前为止我有以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- <!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css/styles.css" rel="stylesheet">
<!--<link rel="stylesheet" href="mail.css"/> -->
</head>
<body>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
/* connect to gmail */
$user = '*****';
$password = '*********';
$mailbox = "{imap.gmail.com:993/imap/ssl}INBOX";
?>
<?php
$mbx = imap_open($mailbox , $user , $password);
/*imap_check returns information about the mailbox
including mailbox name and number of messages*/
//$check = imap_check($mbx);
/* grab emails */
$emails = imap_search($mbx, 'SINCE "10 Mar 2015"');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
?>
<table class="table table-bordered table-hover">
<tr>
<th>From</th>
<th>Date</th>
<th>Subject</th>
</tr>
<?php
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overviews = imap_fetch_overview($mbx,$email_number,0);
/*imap_fetch_overview returns an overview for a message.
An overview contains information such as message subject,
sender, date, and if it has been seen. Note that it does
not contain the body of the message. Setting the second
parameter to "1:n" will cause it to return a sequence of messages*/
//$overviews = imap_fetch_overview($mbx,"1:{$check->Nmsgs}");
?>
<?php
/* put the newest emails on top.Doesnt work.*/
// rsort($overviews);
rsort($overviews);
print_r($overviews);
foreach($overviews as $overview)
{
?>
<tr>
<td><?php echo $overview->from; ?></td>
<td><?php echo $overview->date; ?></td>
<td><a href="open.php?id=<?php echo $overview->uid; ?>"><?php echo $overview->subject; ?></a></td>
</tr>
<?php
}
}
}
?>
</table>
</body>
</html>
我在此处看到 post How can I sort arrays and data in PHP?,但我很难理解如何对 $overviews[date] 进行排序。 rsort 或任何其他类型不起作用。您如何指定要在 [date] property.thanks.
排序P.S。这是数组:
Array ( [0] => stdClass Object ( [subject] => Fwd: A Short Course STI #4653 [from] => Fran ***8olo [to] => Fran ****lo [date] => Tue, 10 Mar 2015 12:42:46 GMT [message_id] => <-7376330247335926430@unknownmsgid> [size] => 28928 [uid] => 1532 [msgno] => 743 [recent] => 0 [flagged] => 0 [answered] => 0 [deleted] => 0 [seen] => 0 [draft] => 0 [udate] => 1425991366 ) )
使用 usort 更新代码:
<?php
/* grab emails */
$emails = imap_search($mbx, 'SINCE "10 Mar 2015"');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
?>
<table class="table table-bordered table-hover">
<tr>
<th>From</th>
<th>Date</th>
<th>Subject</th>
</tr>
<?php
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overviews = imap_fetch_overview($mbx,$email_number,0);
/*imap_fetch_overview returns an overview for a message.
An overview contains information such as message subject,
sender, date, and if it has been seen. Note that it does
not contain the body of the message. Setting the second
parameter to "1:n" will cause it to return a sequence of messages*/
//$overviews = imap_fetch_overview($mbx,"1:{$check->Nmsgs}");
?>
<?php
/* put the newest emails on top.Doesnt work.*/
// rsort($overviews);
usort($overviews, function($a1, $a2) {
$v1 = strtotime($a1['date']);
$v2 = strtotime($a2['date']);
return $v1 - $v2; // $v2 - $v1 to reverse direction
});
print_r($overviews);
foreach($overviews as $overview)
{
?>
<tr>
<td><?php echo $overview->from; ?></td>
<td><?php echo $overview->date; ?></td>
<td><a href="open.php?id=<?php echo $overview->uid; ?>"><?php echo $overview->subject; ?></a></td>
</tr>
<?php
}
}
}
?>
</table>
当我检索电子邮件时仍然随机排序。
您可以在 php
中使用 USORTusort($overviews, function($a1, $a2) {
$v1 = strtotime($a1['date']);
$v2 = strtotime($a2['date']);
return $v1 - $v2; // $v2 - $v1 to reverse direction
});
我修复了它,只需使用 Php 内置的 rsort() 函数即可。
<?php rsort($emails); ?>