Created
August 9, 2012 01:59
-
-
Save lighty/3300282 to your computer and use it in GitHub Desktop.
PHPでRDBから取得した配列データをコントロールブレイクでまとめたいなーと思って作って日の目を見なかったfunction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************** | |
// 配列のグループ化処理 | |
// | |
// 指定したキーで、配列を以下の例の様にグルーピングする | |
// [ | |
// [ "id"=>1, "valueA"=>"aaa", "valueB"=>"bbb"], | |
// [ "id"=>1, "valueA"=>"ccc", "valueB"=>"ddd"], | |
// ] | |
// | |
// ↓↓↓↓↓↓↓↓ | |
// | |
// [ | |
// "1" => [ | |
// [ "valueA"=>"aaa", "valueB"=>"bbb"], | |
// [ "valueA"=>"aaa", "valueB"=>"bbb"], | |
// ], | |
// ] | |
// | |
//******************************** | |
function arrayGroupingValue($records, $keyArray){ | |
$retArray = array(); | |
$workArray = array(); | |
foreach($records as $record){ | |
foreach($keyArray as $key){ | |
// ブレイクキーとなるカラムの値をworkArrayに保持している | |
$workArray[$key] = $record[$key]; | |
} | |
// レコードを登録する処理 | |
$tmpArray = &$retArray; | |
foreach($workArray as $key => $value){ | |
$tmpArray = &$tmpArray[$value]; | |
} | |
$tmpArray[] = $record; | |
} | |
return $retArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment