0

我可能会以错误的方式解决这个问题,这就是我想要做的:

我有 2 个数组,都来自 Jira 错误系统,一个用于注释,另一个用于附件,从 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 
    )

现在评论:

$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个将匹配,因为两个数组都有,所有其他数组都有附件或评论的值,但不是两者都有,然后一旦我有1个大数组,我可以按纪元对其进行排序并按时间顺序回显结果

那行得通吗?如果可以的话,php 中的哪种合并命令最好,array_merge 还是递归的?我可能过于复杂了,我是 php 新手,所以我有点卡在最好的方向上

4

1 回答 1

2

哇,我以前尝试过很多没有用的东西,但我试过了,效果很好。我只是在等待答案时胡闹,没想到会那么容易

我更新了两个数组以使用公共 jira_epoch 变量:

$comments = $issue_json->fields->comment->comments;
        $result = array();

        foreach ($comments as $comment) 
            {
            $result[] = array
                (
                'comments_date' => convert_time($comment->updated),
                'jira_epoch' => convert_sql_time($comment->updated),
                'comments_displayName' => $comment->author->displayName,
                'comments_body' => $comment->body,
                );
            }

        $attachments = $issue_json->fields->attachment;
        $result1 = array();

        foreach ($attachments as $attachment)
            {
            $result1[] = array
                (
                'attachment_date' => convert_time($attachment->created),
                'jira_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,
                );
            }

然后我找到了一些代码,它似乎工作得很好

$test = array_merge($result,$result1);
            function sortByOrder($a, $b) {
    return $a['jira_epoch'] - $b['jira_epoch'];
}

usort($test, 'sortByOrder');
于 2012-11-25T11:22:21.737 回答