如何在php中访问数组的单值数组

时间:2017-03-13 07:04:41

标签: php arrays json imap

我想在以下代码中访问数组中的单个数组值

现在结果:

{"result":[{"msgno":" 1","from":[{"personal":"Blind Mailer","mailbox":"mailerblind","host":"gmail.com"}],"subject":"Testing","date":"Mon, 2 Jan 2017 13:16:21 +0530"}]}

必填结果:

{"result":[{"msgno":" 1","from":"Blind Mailer","subject":"Testing","date":"Mon, 2 Jan 2017 13:16:21 +0530"}]}

php代码:

$emails = imap_search($inbox,'SEEN'); 
$output = '';
$result=array(); 
foreach($emails as $ove) {

$headerInfo = imap_headerinfo($inbox,$ove);

$emailStructure = imap_fetchstructure($inbox,$ove);

if(!isset($emailStructure->multipart)) {
    array_push($result,array("msgno"=>$headerInfo->Msgno ,"from"=>$headerInfo->from,"subject"=>$headerInfo->subject,"date"=>$headerInfo->date));
} else {   
     array_push($result,array("msgno"=>$ove->msgno,"from"=>$ove->from,"subject"=>$ove->subject,"date"=>$ove->date));
}   
}
 echo json_encode(array('result'=>$result));

1 个答案:

答案 0 :(得分:0)

array_push会将项目附加到数组中。由于你追加的是一个数组,你在一个数组中有一个数组而不是一个数组中的所有值。

相反,您希望使用array_merge,它将采用两个数组并将它们连接在一起以创建单个1维数组。请参阅PHP手册:http://php.net/manual/en/function.array-merge.php