如何将不同的数组值与公共密钥组合在一起

时间:2015-11-02 08:41:45

标签: php arrays

我有一个这样的数组:

Array
(
    [0] => Array
        (
            [minutesPlayed] => 0
            [totalSecondsPlayed] => 0
            [flagrantFouls] => 0
            [foulsReceived] => 0
            [blocksReceived] => 0
            [plusMinus] => 0
            [player] => Array
                (
                    [playerId] => 830651
                    [firstName] => Walter
                    [lastName] => Tavares
                    [uniform] => 21
                )

            [fieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

            [freeThrows] => Array
                (
                    [made] => 12
                    [attempted] => 4
                )

            [threePointFieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

        )

    [1] => Array
        (
            [minutesPlayed] => 0
            [totalSecondsPlayed] => 0
            [flagrantFouls] => 0
            [foulsReceived] => 0
            [blocksReceived] => 0
            [plusMinus] => 0
            [player] => Array
                (
                    [playerId] => 830651
                    [firstName] => John
                    [lastName] => Tavares
                    [uniform] => 22
                )

            [fieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

            [freeThrows] => Array
                (
                    [made] => 12
                    [attempted] => 6
                )

            [threePointFieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )
        )

    [2] => Array
        (
            [minutesPlayed] => 0
            [totalSecondsPlayed] => 0
            [flagrantFouls] => 0
            [foulsReceived] => 0
            [blocksReceived] => 0
            [plusMinus] => 0
            [player] => Array
                (
                    [playerId] => 830651
                    [firstName] => Adrian
                    [lastName] => Tavares
                    [uniform] => 23
                )

            [fieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

            [freeThrows] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

            [threePointFieldGoals] => Array
                (
                    [made] => 12
                    [attempted] => 8
                )
        )
    [3] => Array
        (
            [minutesPlayed] => 0
            [totalSecondsPlayed] => 0
            [flagrantFouls] => 0
            [foulsReceived] => 0
            [blocksReceived] => 0
            [plusMinus] => 0
            [player] => Array
                (
                    [playerId] => 830651
                    [firstName] => Adrian
                    [lastName] => Methue
                    [uniform] => 24
                )

            [fieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

            [freeThrows] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )

            [threePointFieldGoals] => Array
                (
                    [made] => 0
                    [attempted] => 0
                )
    )
)

我希望它是这样的:

Array
(
    [fieldGoals] => Array
            [0](
                    [player_name]=>Walter Tavares
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
            [1](
                    [player_name]=>John Tavares
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
            [2](
                    [player_name]=>Adrian Tavares
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
            [3](
                    [player_name]=>Adrian Methue
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
    [freeThrows] => Array
            [0](
                    [player_name]=>Walter Tavares
                    [playerId] => 830651
                    [made] => 12
                    [attempted] => 4
            )
            [1](
                    [player_name]=>John Tavares
                    [playerId] => 830651
                    [made] => 12
                    [attempted] => 6
            )
            [2](
                    [player_name]=>Adrian Tavares
                    [playerId] => 830651
                    [made] => 12
                    [attempted] => 8
            )
            [3](
                    [player_name]=>Adrian Methue
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
    [threePointFieldGoals] => Array
            [0](
                    [player_name]=>Walter Tavares
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
            [1](
                    [player_name]=>John Tavares
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
            [2](
                    [player_name]=>Adrian Tavares
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )
            [3](
                    [player_name]=>Adrian Methue
                    [playerId] => 830651
                    [made] => 0
                    [attempted] => 0
            )

)

这是我到目前为止所做的:

foreach($myArr as $playerStatsKey=>$playerStatsArray){
                 if(!is_array($playerStatsArray)){
                     continue;
                }
            foreach($playerStatsArray as $playkey=>$playVal){
                 if(!is_array($playVal)){
                     continue;
                }
                if($playkey=='player'){
                            $playerInfo[$playkey]['made'] = $playVal['made'];
                            $playerInfo[$playkey]['attempted'] = $playVal['attempted'];
                    }
                    $arr[$playkey] = $playerInfo;
                    $arr[$playkey] = $playVal['made'];
                    $arr[$playkey] = $playVal['attempted'];

                }
                echo '<pre>' ;print_r($arr  );   

我只想将不同的数组值与公共键组合在一起。我能实现这个吗?

2 个答案:

答案 0 :(得分:2)

你在这里。这个功能可以完成你的任务:

function combinePlayers( $array ) {
    $return_array = array();
    foreach ( $array as $element ) {
        //collect data for the new player object
        $player = array(
            'player_name' => $element['player']['firstName'] . ' ' . $element['player']['lastName'],
            'playerId'    => $element['player']['playerId']
        );
        foreach ( $element as $key => $value ) {
            if ( is_array( $value ) && $key != 'player' ) {
                //collect the keys to build the structure of the $return_array
                if ( ! array_key_exists( $key, $return_array ) ) {
                    $return_array[ $key ] = array();
                }
                //collect the returning values from the input array
                array_push( $return_array[ $key ], array_merge( $value, $player ) );
            }
        }
    }
    return $return_array;
}

此函数循环输入数组并收集不是播放器信息的数组值。同样在迭代中它创建一个新的播放器对象,它将合并到所有值。

答案 1 :(得分:1)

Try like this..
It will return the combined result array as you want. I hope this will help.

foreach($myArr as $playerStatsKey=>$playerStatsArray){
    $arr['fieldGoals'][] = array(
                                'player_name'=>$playerStatsArray['player']['firstName']." ".$playerStatsArray['player']['lastName'],
                                'playerId'=>$playerStatsArray['player']['playerId'],
                                'made'=>$playerStatsArray['fieldGoals']['made'],
                                'attempted'=>$playerStatsArray['fieldGoals']['attempted']
                                );

    $arr['freeThrows'][] = array(
                                'player_name'=>$playerStatsArray['player']['firstName']." ".$playerStatsArray['player']['lastName'],
                                'playerId'=>$playerStatsArray['player']['playerId'],
                                'made'=>$playerStatsArray['freeThrows']['made'],
                                'attempted'=>$playerStatsArray['freeThrows']['attempted']
                                );

    $arr['threePointFieldGoals'][] = array(
                                'player_name'=>$playerStatsArray['player']['firstName']." ".$playerStatsArray['player']['lastName'],
                                'playerId'=>$playerStatsArray['player']['playerId'],
                                'made'=>$playerStatsArray['threePointFieldGoals']['made'],
                                'attempted'=>$playerStatsArray['threePointFieldGoals']['attempted']
                                );
 }
echo '<pre>' ;print_r($arr );