PHPは配列通信録をアルファベットA-Zで並べ替える
9171 ワード
通信録のシーンを開発すると、練習連絡先情報がアルファベットa-zでソートされます.
class contact{
/**
*配列の初期化
* @var [type]
*/
private $user=[
0=>['id'=>1,'username'=>'パンダ酒仙','phone'=>'132323232','sex'=>'男']
1=>['id'=>2,'username'=>'小黒','phone'=>'132323232','sex'=>'男']
2=>['id'=>3,'username'=>'流行','phone'=>'132323232','sex'=>'男']
3=>['id'=>4,'username'=>'復讐の魂','phone'=>'132323232','sex'=>'男']
4=>['id'=>5,'username'=>'サル','phone'=>'132323232','sex'=>'男']
5=>['id'=>5,'username'=>'死の預言者','phone'=>'132323232','sex'=>'男']
6=>['id'=>7,'username'=>'末日の使者','phone'=>'132323232','sex'=>'男']
7=>['id'=>8,'username'=>'ステルス刺客','phone'=>'132323232','sex'=>'男']
8=>['id'=>9,'username'=>'巫女','phone'=>'132323232','sex'=>'男']
9=>['id'=>10,'username'=>'氷龍','phone'=>'132323232','sex'=>'男']
10=>['id'=>11,'username'=>'龍騎士','phone'=>'132323232','sex'=>'男']
11=>['id'=>12,'username'=>'白虎','phone'=>'132323232','sex'=>'男']
12=>['id'=>13,'username'=>'ルナ','phone'=>'132323232','sex'=>'男']
13=>['id'=>14,'username'=>'剣聖','phone'=>'132323232','sex'=>'男']
];
/**
*配列をアルファベットA-Zで並べ替える
* @return [type] [description]
*/
public function chartSort(){
$user=$this->user;
foreach ($user as $k => &$v) {
$v['chart']=$this->getFirstChart( $v['username'] );
}
$data=[];
foreach ($user as $k => $v) {
if ( empty( $data[ $v['chart'] ] ) ) {
$data[ $v['chart'] ]=[];
}
$data[ $v['chart'] ][]=$v;
}
ksort($data);
return $data;
}
/**
*最初の漢字の頭文字を返します
* @param [type] $str [string]
* @return [type] [strind]
*/
public function getFirstChart($str){
if( empty($str) ){
return '';
}
$char=ord($str[0]);
if( $char >= ord('A') && $char <= ord('z') ){
return strtoupper($str[0]);
}
$s1=iconv('UTF-8','gb2312',$str);
$s2=iconv('gb2312','UTF-8',$s1);
$s=$s2==$str?$s1:$str;
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319&&$asc<=-20284) return 'A';
if($asc>=-20283&&$asc<=-19776) return 'B';
if($asc>=-19775&&$asc<=-19219) return 'C';
if($asc>=-19218&&$asc<=-18711) return 'D';
if($asc>=-18710&&$asc<=-18527) return 'E';
if($asc>=-18526&&$asc<=-18240) return 'F';
if($asc>=-18239&&$asc<=-17923) return 'G';
if($asc>=-17922&&$asc<=-17418) return 'H';
if($asc>=-17417&&$asc<=-16475) return 'J';
if($asc>=-16474&&$asc<=-16213) return 'K';
if($asc>=-16212&&$asc<=-15641) return 'L';
if($asc>=-15640&&$asc<=-15166) return 'M';
if($asc>=-15165&&$asc<=-14923) return 'N';
if($asc>=-14922&&$asc<=-14915) return 'O';
if($asc>=-14914&&$asc<=-14631) return 'P';
if($asc>=-14630&&$asc<=-14150) return 'Q';
if($asc>=-14149&&$asc<=-14091) return 'R';
if($asc>=-14090&&$asc<=-13319) return 'S';
if($asc>=-13318&&$asc<=-12839) return 'T';
if($asc>=-12838&&$asc<=-12557) return 'W';
if($asc>=-12556&&$asc<=-11848) return 'X';
if($asc>=-11847&&$asc<=-11056) return 'Y';
if($asc>=-11055&&$asc<=-10247) return 'Z';
return null;
}
/**
*印刷関数のフォーマット
* @param [type] $str [description]
* @return [type] [description]
*/
public function p($str){
echo '
}
}
オブジェクトの宣言
$contactObj = new contact();
ソートメソッドchartSort()を呼び出し、結果$resultを取得します.
$result=$contactObj->chartSort();
フォーマット印刷関数出力結果
$contactObj->p($result);
以下は結果
class contact{
/**
*配列の初期化
* @var [type]
*/
private $user=[
0=>['id'=>1,'username'=>'パンダ酒仙','phone'=>'132323232','sex'=>'男']
1=>['id'=>2,'username'=>'小黒','phone'=>'132323232','sex'=>'男']
2=>['id'=>3,'username'=>'流行','phone'=>'132323232','sex'=>'男']
3=>['id'=>4,'username'=>'復讐の魂','phone'=>'132323232','sex'=>'男']
4=>['id'=>5,'username'=>'サル','phone'=>'132323232','sex'=>'男']
5=>['id'=>5,'username'=>'死の預言者','phone'=>'132323232','sex'=>'男']
6=>['id'=>7,'username'=>'末日の使者','phone'=>'132323232','sex'=>'男']
7=>['id'=>8,'username'=>'ステルス刺客','phone'=>'132323232','sex'=>'男']
8=>['id'=>9,'username'=>'巫女','phone'=>'132323232','sex'=>'男']
9=>['id'=>10,'username'=>'氷龍','phone'=>'132323232','sex'=>'男']
10=>['id'=>11,'username'=>'龍騎士','phone'=>'132323232','sex'=>'男']
11=>['id'=>12,'username'=>'白虎','phone'=>'132323232','sex'=>'男']
12=>['id'=>13,'username'=>'ルナ','phone'=>'132323232','sex'=>'男']
13=>['id'=>14,'username'=>'剣聖','phone'=>'132323232','sex'=>'男']
];
/**
*配列をアルファベットA-Zで並べ替える
* @return [type] [description]
*/
public function chartSort(){
$user=$this->user;
foreach ($user as $k => &$v) {
$v['chart']=$this->getFirstChart( $v['username'] );
}
$data=[];
foreach ($user as $k => $v) {
if ( empty( $data[ $v['chart'] ] ) ) {
$data[ $v['chart'] ]=[];
}
$data[ $v['chart'] ][]=$v;
}
ksort($data);
return $data;
}
/**
*最初の漢字の頭文字を返します
* @param [type] $str [string]
* @return [type] [strind]
*/
public function getFirstChart($str){
if( empty($str) ){
return '';
}
$char=ord($str[0]);
if( $char >= ord('A') && $char <= ord('z') ){
return strtoupper($str[0]);
}
$s1=iconv('UTF-8','gb2312',$str);
$s2=iconv('gb2312','UTF-8',$s1);
$s=$s2==$str?$s1:$str;
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319&&$asc<=-20284) return 'A';
if($asc>=-20283&&$asc<=-19776) return 'B';
if($asc>=-19775&&$asc<=-19219) return 'C';
if($asc>=-19218&&$asc<=-18711) return 'D';
if($asc>=-18710&&$asc<=-18527) return 'E';
if($asc>=-18526&&$asc<=-18240) return 'F';
if($asc>=-18239&&$asc<=-17923) return 'G';
if($asc>=-17922&&$asc<=-17418) return 'H';
if($asc>=-17417&&$asc<=-16475) return 'J';
if($asc>=-16474&&$asc<=-16213) return 'K';
if($asc>=-16212&&$asc<=-15641) return 'L';
if($asc>=-15640&&$asc<=-15166) return 'M';
if($asc>=-15165&&$asc<=-14923) return 'N';
if($asc>=-14922&&$asc<=-14915) return 'O';
if($asc>=-14914&&$asc<=-14631) return 'P';
if($asc>=-14630&&$asc<=-14150) return 'Q';
if($asc>=-14149&&$asc<=-14091) return 'R';
if($asc>=-14090&&$asc<=-13319) return 'S';
if($asc>=-13318&&$asc<=-12839) return 'T';
if($asc>=-12838&&$asc<=-12557) return 'W';
if($asc>=-12556&&$asc<=-11848) return 'X';
if($asc>=-11847&&$asc<=-11056) return 'Y';
if($asc>=-11055&&$asc<=-10247) return 'Z';
return null;
}
/**
*印刷関数のフォーマット
* @param [type] $str [description]
* @return [type] [description]
*/
public function p($str){
echo '
';
print_r($str);
echo '
'; }
}
オブジェクトの宣言
$contactObj = new contact();
ソートメソッドchartSort()を呼び出し、結果$resultを取得します.
$result=$contactObj->chartSort();
フォーマット印刷関数出力結果
$contactObj->p($result);
以下は結果
Array
(
[B] => Array
(
[0] => Array
(
[id] => 10
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => B
)
[1] => Array
(
[id] => 12
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => B
)
)
[F] => Array
(
[0] => Array
(
[id] => 3
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => F
)
[1] => Array
(
[id] => 4
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => F
)
)
[L] => Array
(
[0] => Array
(
[id] => 11
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => L
)
[1] => Array
(
[id] => 13
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => L
)
[2] => Array
(
[id] => 13
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => L
)
)
[M] => Array
(
[0] => Array
(
[id] => 7
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => M
)
)
[S] => Array
(
[0] => Array
(
[id] => 5
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => S
)
[1] => Array
(
[id] => 5
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => S
)
)
[W] => Array
(
[0] => Array
(
[id] => 9
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => W
)
)
[X] => Array
(
[0] => Array
(
[id] => 1
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => X
)
[1] => Array
(
[id] => 2
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => X
)
)
[Y] => Array
(
[0] => Array
(
[id] => 8
[username] =>
[phone] => 13232323232
[sex] =>
[chart] => Y
)
)
)