duanchijie2323 2018-01-12 00:17
浏览 10
已采纳

如何在PHP中基于键转换数组?

I am trying to divide an array based on values within the array, and to sort it based on keys that the array also has. My array currently looks like this:

array:6 [▼
"player_ids" => array:8 [▼
0 => "103"
1 => "221"
2 => "283"
3 => "321"
4 => "333"
5 => "406"
6 => "425"
7 => "428"
]
"game_id" => array:8 [▼
103 => "33058041"
221 => "33058041"
283 => "33058041"
321 => "33058041"
333 => "33058041"
406 => "33058041"
425 => "33058041"
428 => "33058041"
]
"goals" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"assists" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"yellows" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"red" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
]

with arrays with each of the players embedded in a particular field. How would I manipulate this array to make it so that player_ids would become the first key, like so:

"103" => array:6 [▼
player_ids => "103"
game_id => "33058041"
goals => "0"
assists => "0"
yellows => "0"
red => "0"
]

for each of the player id's listed?

  • 写回答

2条回答 默认 最新

  • doujingtang6580 2018-01-12 00:35
    关注

    you can loop through the player_ids key in your big array and create a new array with the structure you want. Something like this.

    $newArray = array();
    foreach ($array['player_ids'] as $p){
       $newArray[$p] = array (
                        'player_ids' => $p,
                        'game_id' => $array['game_id'][$p],
                        'goals' => $array['goals'][$p],
                        'assists' => $array['assists'][$p],
                        'yellows' => $array['yellows'][$p],
                        'red' => $array['red'][$p]
                    )
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播