[MySQL]50音判定

REGEXPはマルチバイトに対応していないので、FIND_IN_SETを使用。

INSERT INTO xxx(initial_chara,gyo)
(
SELECT
SUBSTRING(tmp.name,1,1),
(CASE
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'あ,い,う,え,お') THEN 'あ'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'か,き,く,け,こ,が,ぎ,ぐ,げ,ご') THEN 'か'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'さ,し,す,せ,そ,ざ,じ,ず,ぜ,ぞ') THEN 'さ'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'た,ち,つ,て,と,だ,ぢ,づ,で,ど') THEN 'た'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'な,に,ぬ,ね,の') THEN 'な'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'は,ひ,ふ,へ,ほ,ば,び,ぶ,べ,ぼ') THEN 'は'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'ま,み,む,め,も') THEN 'ま'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'や,ゐ,ゆ,ゑ,よ') THEN 'や'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'ら,り,る,れ,ろ') THEN 'ら'
WHEN FIND_IN_SET(SUBSTRING(tmp.name,1,1),'わ,を,ん') THEN 'わ'
ELSE 'ん' END
)
FROM yyy AS tmp

[CodeIgniter]get_table_names_by_dbname

$this->db->list_tables();

上記だとキャッシュから取得する。キャッシュを回避する場合下記を使用。

public function get_table_names_by_dbname($dbname)
{
    $names = array();
    $sql = "SHOW TABLES FROM {$dbname}";
    $query = $this->db->query($sql);
    foreach ($query->result_array() as $row)
    {
        if ( ! isset($key))
        {
            if (isset($row['table_name']))
            {
                $key = 'table_name';
            }
            elseif (isset($row['TABLE_NAME']))
            {
                $key = 'TABLE_NAME';
            }
            else
            {
                $key = array_keys($row);
                $key = array_shift($key);
            }
        }

        $names[] = $row[$key];
    }
    return $names;
}

[CodeIgniter]set_header

$this->output->set_header('X-Frame-Options: SAMEORIGIN');
$this->output->set_header('Cache-control: no-cache, no-store, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header('Expires: Thu, 01 Dec 1994 16:00:00 GMT');

[PHP]zip

public function zip($zipfile,$add_file)
{
    $zip = new ZipArchive();
    $result = $zip->open($zipfile, ZipArchive::CREATE);
    if($result === true)
    {
        $zip->addFile($add_file);
        $result = $zip->close();
    }
    return $result === true;
}

[AWS]FILE権限

AmazonRDSは仕様上FILE権限は付与出来ない。
故に

LOAD DATA INFILE xxxx.csv INTO TABLE wp_names FIELDS TERMINATED BY ',' ENCLOSED BY '\"' IGNORE 1 LINES;

LOAD DATA LOCAL INFILE xxxx.csv INTO TABLE wp_names FIELDS TERMINATED BY ',' ENCLOSED BY '\"' IGNORE 1 LINES;

へ変更するとよい

※余談
windows上ではパスのセパレータを/に変換しないと動かないことも

$filepath = preg_replace('/\\\\/','/',$filepath);

[PHP]echo_json

function echo_json($data)
{
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($data);
}

[PHP]is_https

public function is_https()
{
    if(!empty($_SERVER["HTTPS"]))
        return true;
    //AWS
    if(!empty($_SERVER["HTTP_X_FORWARDED_PROTO"]) && strtolower($_SERVER["HTTP_X_FORWARDED_PROTO"])==="https")
        return true;
    return false;
}

[PHP]array_group_by

SQLのGROUP BYの様な処理を配列に行う

public function array_group_by($arr,$keys = array(array('id','address'),array('name') ),$index = 0)
{
    if(empty($arr))
        return null;
    $results = array();
    $children = array();
    $_vkey = $keys[$index][0];
    $_keys = array_slice($keys,0,$index+1);
    $is_final = count($keys) <= $index + 1;
    $id = null;
    array_push($arr,null);
    foreach ($arr as $i => $data){
        $current_id = $this->get_values_by_keys($data,$_keys,'');
        if($id != $current_id){
            if(!is_null($id)) {
                if(!empty($children)) {
                    $tmp = $this->get_std_by_array($children[0], $keys[$index]);
                    $tmp->value = $children[0]->$_vkey;
                }else{
                    $tmp = new stdClass();
                }
                $tmp->children = $is_final ? $children : $this->array_group_by($children,$keys,$index+1);
                $results[] = $tmp;
                $children = array();
            }
            $id = $current_id;
        }
        $children[] = $data;
    }
    return $results;
}
public function get_std_by_array($arr,$include_keys = null)
{
    $tmp = new stdClass();
    foreach ($arr as $key => $v){
        if(empty($include_keys) || (!empty($include_keys) && in_array($key,$include_keys)))
            $tmp->$key = $v;
    }
    return $tmp;
}
public function get_values_by_keys($data,$keys,$glue = null)
{
    if(empty($data))
        return null;
    $tmp = array();
    foreach ($keys as $key){
        if(is_array($key))
            $key = array_shift(array_values($key));
        $tmp[] = $data->$key;
    }
    return is_null($glue) ? $tmp : implode($glue,$tmp);
}