<?php class inputManager{ private $index = 0; private $input = []; //区切り文字設定 const SPRIT_STR = " "; /** * コンストラクタ * * @access public * @param なし * @return なし */ public function __construct($testFlg = false, $testInput = null){ if(!$testFlg){ $input_lines = trim(fgets(STDIN)); $input_lines = str_replace(array("\r\n","\r","\n"), "", $input_lines); do { $this->input[] = $input_lines; $input_lines = trim(fgets(STDIN)); $input_lines = str_replace(array("\r\n","\r","\n"), "", $input_lines); } while ($input_lines !== ""); }else{ $this->input = $testInput; } } /** * value * 現在のインデックスの値を取得する * * @access public * @param なし * @return string|null 現在のインデックスの値 */ public function value(){ if($this->index < $this->length()){ return $this->input[$this->index]; }else{ return null; } } /** * explodeValue * 現在のインデックスの値をexplodeした配列を取得する * * @access public * @param なし * @return Array|null explode後配列 */ public function explodeValue(){ if($this->index < $this->length()){ return explode(self::SPRIT_STR, $this->input[$this->index]); }else{ return null; } } /** * setindex * インデックスを設定する * * @access public * @param $i_index 設定するインデックス * @return $this */ public function setindex(int $i_index){ $this->index = $i_index; return $this; } /** * next * インデックスを1つすすめる * * @access public * @param なし * @return $this */ public function next(){ $this->index++; return $this; } /** * length * 入力の長さを取得 * * @access public * @param なし * @return int $this->input 配列の長さ */ public function length(){ return count($this->input); } } class out{ /** * output * 引数を出力 * 入力がない場合は空行を出力する * * @access public * @param string $i_val 出力する文字列 * @return なし */ public static function output(string $i_val = ""){ echo $i_val. "\n"; } } //テスト用入力 $input[] = "123"; $input[] = "456"; $input[] = "789"; $input[] = "aaa"; $input[] = "bbb"; $io = new inputManager(true,$input); //長さの取得 out::output($io->length()); // out::output(); //インデックス設定の確認 out::output($io->value()); $io->next(); out::output($io->value()); $io->setindex(2); out::output($io->value()); out::output($io->next()->value()); out::output($io->next()->value()); out::output(); //入力を末尾まで出力する $io->setindex(0); while(!is_null($io->value())){ out::output($io->value()); $io->next(); } out::output(); //インデックスをセットして末尾まで出力する for($i = 0;$i < $io->length(); $i++){ out::output($io->setindex($i)->value()); }
【PHP】連想配列とかオブジェクトのソート
多次元連想配列やオブジェクトを詰めた配列はusort関数でいい感じにソートする。
PHP: usort - Manual
多次元連想配列のソートをする場合
<?php //連想配列でのソート $test = [["price" => "30", "stock" => "100"], ["price" => "20", "stock" => "120"], ["price" => "10", "stock" => "115"]]; //ソート usort($test, function ($a, $b) { return $a['price'] < $b['price'] ? -1 : 1; //逆順の場合はこっち //return $a['price'] > $b['price'] ? -1 : 1; }); print_r($test);
オブジェクトを詰めた配列のソートをする場合
<?php class testClass{ public $price; public $stock; function __construct($price,$stock){ $this->price = $price; $this->stock = $stock; } } $test2 = []; $test2[] = new testClass("30","100"); $test2[] = new testClass("20","120"); $test2[] = new testClass("10","115"); usort($test2, function ($a, $b) { return $a->stock< $b->stock? -1 : 1; }); print_r($test2);
【PHP】文字列を配列に格納する(マルチバイト対応)
PHPのstr_splitにマルチバイト版がなかったので書いた。
<?php /** * mb_str_split * str_splitのマルチバイト版 * * @access public * @param string $string 分割する文字列 * @param int $split_length 分割後の文字数 * @return array $retAry 分割後の配列 * @throws $split_lengthの値が0以下の場合Warning発生 * ※str_split準拠 */ function mb_str_split($string, $split_length = 1){ // 0以下が指定された場合Warningを発生させる if($split_length <= 0){ trigger_error("mb_str_split(): The length of each segment must be greater than zero", E_USER_WARNING); } // 文字列を配列に分解 $aryString = preg_split("//u", $string, -1, PREG_SPLIT_NO_EMPTY); // 文字数の指定がない場合、配列の要素数が0の場合はこのまま返す if($split_length === 1 || count($aryString) === 0){ return $aryString; } // 引数の文字列毎に配列に詰め直す $tmpVal = ""; foreach($aryString as $key => $value){ if(($key + 1) % $split_length !== 0){ $tmpVal = $tmpVal.$value; }else{ $retAry[] = $tmpVal.$value; $tmpVal = ""; } } // 余りの文字列があれば配列に足す if($tmpVal !== ""){ $retAry[] = $tmpVal; } return $retAry; } var_dump(mb_str_split("吾輩は猫である。名前はまだ無い。")); var_dump(mb_str_split("This is a pen.")); var_dump(mb_str_split("吾輩は猫である。名前はまだ無い。。", 3)); var_dump(mb_str_split("This is a pen.", 3));