简体   繁体   English

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

[英]how to access single value array of array in php

I want to access single value of array in array for my following code, 我想为以下代码访问array中array的单个值,

now result: 现在结果:

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

required result: 所需结果:

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

php code: 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));

array_push will append items to the array. array_push将项目添加到数组。 Since what you are appending is itself an array you have an array inside an array rather than all values in a single array. 由于要附加的本身就是一个数组,因此在数组内部有一个数组,而不是单个数组中的所有值。

Instead you want to use array_merge which will take two arrays and join them together to create a single 1-dimensional array. 相反,您想使用array_merge ,它将使用两个数组并将它们连接在一起以创建一个一维数组。 See the PHP manual here: http://php.net/manual/en/function.array-merge.php 请参阅此处的PHP手册: http : //php.net/manual/zh/function.array-merge.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM