下面小編就為大家分享一篇PHP運用foreach神奇的轉換陣列(實例講解),具有很好的參考與學習PHP的價值,希望對大家有幫助。對PHP有興趣的夥伴們一起跟著小編過來看看吧
要求:
##將二維陣列$arr轉換為以'time'和'type'為下標、'data'為值的二維數組;
原始數組:
$arr = array( 0 => array( 'data' => 100, 'type' => 1, 'time' => '2018-01-26', ), 1 => array( 'data' => 200, 'type' => 2, 'time' => '2018-01-26', ), 2 => array( 'data' => 300, 'type' => 2, 'time' => '2018-01-27', ), 3 => array( 'data' => 400, 'type' => 3, 'time' => '2018-01-27', ), 4 => array( 'data' => 500, 'type' => 4, 'time' => '2018-01-28', ), );
轉換:
foreach ($arr as $key => $value) { $change[$value['time']][$value['type']] = $value['data']; }
#結果:##array(3) {
["2018-01-26"] => array(2) {
[1] => int(100)
[2] => int(200)
}
["2018-01-27"] => array(2) {
[2] => int(300)
[3] => int(400)
}
["2018-01-28"] => array(1) {
[4] => int(500)
}
}
以上是PHP運用foreach神奇的轉換數組(實例講解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!