Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将不同的数组值与公共键组合_Php_Arrays - Fatal编程技术网

Php 如何将不同的数组值与公共键组合

Php 如何将不同的数组值与公共键组合,php,arrays,Php,Arrays,我有这样一个数组: Array ( [0] => Array ( [minutesPlayed] => 0 [totalSecondsPlayed] => 0 [flagrantFouls] => 0 [foulsReceived] => 0 [blocksReceived] => 0 [plu

我有这样一个数组:

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
            )

)
我希望它是这样的:

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  );   
foreach($playerStatsKey=>myArr$playerStatsArray){
如果(!是_数组($playerStatsArray)){
继续;
}
foreach($playerStatsArray作为$playkey=>$playVal){
如果(!is_数组($playVal)){
继续;
}
如果($playkey=='player'){
$playerInfo[$playkey]['made']=$playVal['made'];
$playerInfo[$playkey]['Experted']=$playVal['Experted'];
}
$arr[$playkey]=$playerInfo;
$arr[$playkey]=$playVal['made'];
$arr[$playkey]=$playVal['expected'];
}
回音“;打印($arr);
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;
}

我只想用公共键组合不同的数组值。如何实现这一点?

给您。此函数将完成您的任务:

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 );
此函数在输入数组中循环,并收集不属于玩家信息的数组值。此外,在迭代过程中,它还创建了一个新的玩家对象,该对象将合并到所有值中。

如下尝试。。
它将返回您想要的组合结果数组。我希望这会有所帮助。
foreach($myArr作为$playerStatsKey=>$playerStatsArray){
$arr['fieldGoals'][]=阵列(
'player_name'=>$playerStatsArray['player']['firstName']。'.$playerStatsArray['player']['lastName'],
'playerId'=>$playerStatsArray['player']['playerId'],
'made'=>$playerStatsArray['fieldGoals']['made'],
“已尝试”=>$playerStatsArray['fieldGoals']['Expected']
);
$arr['freethows'][]=数组(
'player_name'=>$playerStatsArray['player']['firstName']。'.$playerStatsArray['player']['lastName'],
'playerId'=>$playerStatsArray['player']['playerId'],
'made'=>$playerStatsArray['freetrows']['made'],
“已尝试”=>$playerStatsArray[“罚球”][“已尝试”]
);
$arr['threePointFieldGoals'][]=数组(
'player_name'=>$playerStatsArray['player']['firstName']。'.$playerStatsArray['player']['lastName'],
'playerId'=>$playerStatsArray['player']['playerId'],
'made'=>$playerStatsArray['threePointFieldGoals']['made'],
“已尝试”=>$playerStatsArray['threePointFieldGoals']['Expected']
);
}
回音“;打印($arr);
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;
}

我没有两个不同的数组。正如我在问题中所问,我想根据某个键来分隔数组