实现的PHP关联数组逻辑中的问题

时间:2018-02-28 20:44:41

标签: php

我是php的新手,我有下面的代码和fnction输出如下所示。

public function getNLastDailyBuilds()
    {
        ksort( $this->deployments['list'] );
        $history = array();

        foreach( $this->deployments['list'] as $buildId => $build ) {

            $job = $build->getBuildDetails( $this->teamCIDB, $this->jenkins );
            $infos = $this->jenkins->parseMavenBuildInfo( $job );
            if ( $build->isOngoing() && !$build->getStatus()  ) {
                continue;
            }
            if ( array_key_exists( 'shortDescription', $infos['causes'] ) && ( $infos['causes']['shortDescription'] == 'Started by timer' )) {

                if ( array_key_exists( $build->getDateASString(), $history ) && (($history[$build->getDateASString()]['status'] == 'SUCCESS' ) || ($history[$build->getDateASString()]['status'] == 'UNSTABLE' )))

                    $history[$build->getDateAsString()] = array( 'date' => $build->getDateASString(), 'id' => $buildId, 'Auto' => 1, 'status' => $build->getStatus(), 'description' => $build->getDescription(), 'defect' => $build->getDefectID() );


                else {
                    if ( !array_key_exists( $build->getDateAsString(), $history ))

                        $history[$build->getDateAsString()] = array( 'date' => $build->getDateASString(), 'id' => $buildId, 'Auto' => 1, 'status' => $build->getStatus(), 'description' => $build->getDescription(), 'defect' => $build->getDefectID() );
                }   

            } else {

                if ( !array_key_exists( $build->getDateAsString(), $history ) || ($history[$build->getDateASString()]['status'] == 'ABORTED' ))

                    $history[$build->getDateAsString()] = array( 'date' => $build->getDateASString(), 'id' => $buildId, 'Auto' => 0, 'status' => $build->getStatus(), 'description' => $build->getDescription(), 'defect' => $build->getDefectID() );
            }
        }
        mydebug( "NLAST=".print_r( $history, TRUE ));
        return $history;
    }

它打印输出如下

    NLAST=Array
(   
    [2018-02-27] => Array
        (
            [date] => 2018-02-27
            [id] => 196
            [Auto] => 0
            [status] => FAILURE
            [description] =>
            [defect] => 0
        )

    [2018-02-28] => Array
        (
            [date] => 2018-02-28
            [id] => 197
            [Auto] => 1
            [status] => FAILURE
            [description] =>
            [defect] => 0
        )

)

当我在2018-02-28的日期有多个[id]时,它没有超过写入数组。例如,ksort($ this-> deployments ['list']);会有相同日期的多条目,比如197,198,200。 但结果数组在我们打印时会带来第一个条目,但我需要最后一个来自部署['list']的条目为2018-02-28。

0 个答案:

没有答案