【发布时间】:2012-11-25 11:03:14
【问题描述】:
我可能会以错误的方式解决这个问题,这就是我想要做的:
我有 2 个数组,都来自 Jira 错误系统,一个用于 cmets,另一个用于附件,从 API 中提取时两者都是不同的对象,但我想显示两者的组合并打印出结果,以便按时间顺序显示。
例如现在它的:
comment 1 - 5th April
Comment 2 - 20th April
comment 3 - 9th June
attachment 1 - 5th April
attachment 2 - 20th April
I want to dislay that as:
comment 1 - 5th April
attachment 1 - 5th April
Comment 2 - 20th April
attachment 2 - 20th April
comment 3 - 9th June
好的,这是我的附件数组:
$attachments = $issue_json->fields->attachment;
$result1 = array();
foreach ($attachments as $attachment)
{
$result1[] = array
(
'attachment_date' => convert_time($attachment->created),
'attachment_epoch' => convert_sql_time($attachment->created),
'attachment_displayName' => $attachment->author->displayName,
'attachment_filename' => $attachment->filename,
'attachment_file_type' => $attachment->mimeType,
'attachment_thumbnail' => $attachment->thumbnail,
'attachment_url_link' => $attachment->content,
);
}
现在结果如下:
Array
( [0] => Array(
[attachment_date] => 13th Apr 12 07:10
[attachment_epoch] => 1334301000
[attachment_displayName] => XXX
[attachment_filename] => 1.txt
[attachment_file_type] => text/plain
[attachment_thumbnail] =>
[attachment_url_link] => https://xxx.atlassian.net/secure/attachment/18605/1.txt
)
[1] => Array(
[attachment_date] => 13th Apr 12 07:10
[attachment_epoch] => 1334301000
[attachment_displayName] => xxx
[attachment_filename] => 2.txt
[attachment_file_type] => text/plain
[attachment_thumbnail] =>
[attachment_url_link] => https://xxx.atlassian.net/secure/attachment/18606/2.txt
)
现在是cmets:
$comments = $issue_json->fields->comment->comments;
$result = array();
foreach ($comments as $comment)
{
$result[] = array
(
'comments_date' => convert_time($comment->updated),
'comments_epoch' => convert_sql_time($comment->updated),
'comments_displayName' => $comment->author->displayName,
'comments_body' => $comment->body,
);
}
这是一个输出示例:
Array ( [0] => Array(
[comments_date] => 11th Apr 12 20:52
[comments_epoch] => 1334177520
[comments_displayName] => xxx
[comments_body] => Cannot reproduce, but attempted a fix based on the log file.
)
[1] => Array(
[comments_date] => 13th Apr 12 07:09
[comments_epoch] => 1334300940
[comments_displayName] => xxx
[comments_body] => 1) tested on a bit older version it still happens with it
)
现在我计划做的是合并 2 个数组,但我不确定这是可能的,因为它们没有匹配的变量。
我能不能把它合并成一件大事,然后把我需要的那些合并起来,因为我想按它排序的纪元日期,所以合并它们直到我有这样的东西:
$result1[] = array
(
'date' => convert_time($attachment->created),
'epoch' => convert_sql_time($attachment->created),
'displayName' => $attachment->author->displayName,
'attachment_filename' => $attachment->filename,
'attachment_file_type' => $attachment->mimeType,
'attachment_thumbnail' => $attachment->thumbnail,
'attachment_url_link' => $attachment->content,
'comments_date' => convert_time($comment->updated),
'comments_epoch' => convert_sql_time($comment->updated),
'comments_displayName' => $comment->author->displayName,
'comments_body' => $comment->body,
);
所以前 3 个将匹配,因为两个数组都有,所有其他数组都有附件或 cmets 的值,但不是两者都有,然后一旦我有 1 个大数组,我可以按纪元对其进行排序并按时间顺序回显结果顺序
这行得通吗?如果可以,php 中的哪种合并命令是最好的,array_merge 还是递归的?我可能过于复杂了,我是 php 新手,所以我有点卡在最好的方向上
【问题讨论】:
标签: php object multidimensional-array array-merge