【问题标题】:Two Multidimensional arrays, need to combine to echo out in order by date php两个多维数组,需要按日期php组合起来按顺序回显
【发布时间】: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


    【解决方案1】:

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

    我更新了两个数组以使用通用 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多