2017-03-13 75 views
0

我要訪問數組的單個值在數組我下面的代碼,如何訪問數組的單值數組在PHP

現在的結果是:

{"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)); 

回答

0

array_push將附加條目到數組中。由於你所追加的本身就是一個數組,所以你在數組中有一個數組,而不是單個數組中的所有值。

相反,您希望使用array_merge,它將採用兩個數組並將它們連接在一起以創建單個1維數組。請參閱PHP手冊:http://php.net/manual/en/function.array-merge.php