配列のめも
助けてと頼まれたので、書きますφ(..)メモメモ
助けて先輩!俺のPHPの配列の組み直しは最適なのか確認したい - ITの隊長のブログ
環境
$ node -v v0.10.25
コード
JavaScriptで書いてますが、言語特有の関数とか使ってないし PHPでも似たようなことは出来る(はず)という前提です(-_-;)
流れとしては、未登録の場合は新規追加、 そうでなければ登録済のものを取り出して値を追加としてます。
基本的には、そんな変わらない・・・
変更した点は、エンティティクラスを利用するようにした点ですかね。
table_03
というカラムが、id
に重複がある場合は配列になるようだったので、
最初からデータを配列で持つようにしました。
genreList
という変数のこと。 文字列と配列が混合してしまうようなら、配列で統一したほうが扱いやすいかと思います
補足
あと、元のコードで気になった点が一つ
// 初回のみ$result_arrayは連想配列ではないため、比較するとエラーになる
// なので、配列が空であれば0なので、条件分岐でfalse(初回追加となる)
ループの回数に依存すると、バグの原因になりそうだったので↓のように修正してみました(間違ってたらサーセン)
以下の部分
// for文から外に出すためにここで実行
// 整列した配列を格納したい変数です $result_array = array(); // **************************** // for文から外に出すためにここで実行 // 初回の追加 $empty_array = array(); array_walk_recursive($v, function(&$value, $key) use (&$empty_array) { $empty_array[$key] = $value; }); array_push($result_array, $empty_array); // **************************** // $sql_dataには先程のsql結果のデータが入っています。 foreach($sql_data as $k => $v) { $tmp_array_save = array(); array_walk_recursive($v, function(&$value, $key) use (&$tmp_array_save) { $tmp_array_save[$key] = $value; }); // 返す配列にすでに存在しているかチェック $contents_id_array = Hash::extract($result_array, '{n}.contents_id'); if (in_array($tmp_array_save['contents_id'], $contents_id_array)) { // 存在している場合は重複なので、追加 // contents_idから逆引きして格納が必要な配列番号を探す $iterator = new RecursiveIteratorIterator( new RecursiveArrayIterator($result_array), RecursiveIteratorIterator::SELF_FIRST ); // table03が追加される配列番号を格納する変数 $add_index_number = ''; foreach ($iterator as $iterate_key => $iterate_leaf) { if (is_array($iterate_leaf)) { $add_index_number = $iterate_key; } } // table03の値が重複していないかチェック、していなければ追加 if (! in_array($tmp_array_save['table_03'], $result_array[$add_index_number])) { $tmp_tags_data = $result_array[$add_index_number]['table_03']; $result_array[$add_index_number]['table_03'] = array(); array_push($result_array[$add_index_number]['table_03'], $tmp_array_save['table_03'], $tmp_tags_data); } } else { // 存在していない場合は新規追加 array_push($result_array, $tmp_array_save); } } // 確認コード dump($result_array);
H2の使い方めも
Javaで実装されてるデータベース、H2の使い方めも
H2データベースは、組み込みでもサーバモードでも動作するとのこと
今回は、組み込みで利用したときのめも
- 環境
$ sw_vers ProductName: Mac OS X ProductVersion: 10.10.1 BuildVersion: 14B25
build.gradle
apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' group = 'h2' repositories { mavenCentral() } dependencies { compile "com.h2database:h2:1.4.184" compile "commons-dbutils:commons-dbutils:1.6" }
データベースの作成
jarファイルを実行する
$ java -jar ${PATH_TO_JAR}/h2-1.4.184.jar
自分の場合は、Gradleでビルド時にダウンロードされたjarを指定しました。 Windowsの場合はインストーラを実行する必要があるみたいです
ブラウザが立ち上がりログイン画面が表示されるので、テーブル作成を進める
この辺は以下の記事が参考になるかと思いますm(__)m
実際に使ってみる
ソースはGithubに・・・
気軽にJavaだけで、データベースを使えるので便利ですね(^o^)
参考にしましたm(__)m
grunt-connect-proxyのめも
Gruntで起動したwebサーバにリバースプロキシの設定を追加するめも
プラグインのインストール
$ npm install grunt-connect-proxy --save-dev
設定追加
Gruntfile.js
に追加していく
モジュールの読込
connect:livereload:options:middleware
で読み込める場所に宣言する
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
middleware
の部分に読み込んだモジュールを追加
connect: { options: { port: 9000, open: true, livereload: 35729, // Change this to '0.0.0.0' to access the server from outside hostname: 'localhost' }, livereload: { options: { middleware: function(connect) { return [ // 追加 proxySnippet, connect.static('.tmp'), connect().use('/bower_components', connect.static('./bower_components')), connect.static(config.app) ]; } } }, // ~省略~ }
プロキシの設定を追加
オプションは下記を参照
connect: { options: { port: 9000, // change this to '0.0.0.0' to access the server from outside hostname: 'localhost' }, proxies: [ { // `localhost:9000/cortex`へのリクエストは`localhost:8080/cortex`へプロキシされるようになる context: '/cortex', host: 'localhost', port: 8080 }, // 試しに天気情報を公開しているAPIにプロキシしてみる { context: '/data/', host: 'api.openweathermap.org', port: '80' } ], // ~省略~ }
serve
タスクにconfigureProxies
を追加
grunt.registerTask('serve', 'start the server and preview your app, --allow-remote for remote access', function (target) { if (grunt.option('allow-remote')) { grunt.config.set('connect.options.hostname', '0.0.0.0'); } if (target === 'dist') { return grunt.task.run(['build', 'connect:dist:keepalive']); } grunt.task.run([ 'clean:server', 'wiredep', 'concurrent:server', // 追加 'configureProxies', 'autoprefixer', 'connect:livereload', 'watch' ]); });
実行
webサーバ起動
$ grunt serve
http://localhost:9000/data/2.5/weather?q=Okinawa,jp
へアクセスしたら値が取得できる
実行結果
{ "coord":{ "lon":127.8, "lat":26.34 }, "sys":{ "type":1, "id":7625, "message":0.0345, "country":"JP", "sunrise":1417989865, "sunset":1418027821 }, "weather":[ { "id":500, "main":"Rain", "description":"light rain", "icon":"10n" }, { "id":701, "main":"Mist", "description":"mist", "icon":"50n" } ], "base":"cmc stations", "main":{ "temp":288.78, "pressure":1024, "humidity":100, "temp_min":288.15, "temp_max":290.15 }, "wind":{ "speed":3.6, "deg":50 }, "clouds":{ "all":1 }, "dt":1418055660, "id":1894616, "name":"Okinawa", "cod":200 }
参考にしましたm(__)m
webアプリ開発時の自動化めも(yeoman)
webのフロントエンド開発時の自動化(Yeoman)についてのメモ
webサーバはPCにApacheをインストールして使ってた
ディレクトリとか変えたらApacheの設定も変更が必要...(><)
MacOSのアップデート時にApacheもバージョンアップされて、これまでの設定が無効にorz
2.2 -> 2.4
ライブラリはダウンロードしてディレクトリに配置してた。。。
手動でやるのめんどい(-_-;)
管理もめんどい...
これからは自動化したい
Yeomanに変更後コマンドで出来るように!!
インストール
必要なもの
Node.jsはインストーラがあるのでそれを利用
Grunt, Bower, Yoは下記でインストール
$ sudo npm install -g bower grunt-cli yo
Generatorをインストール。今回はwebapp
Generatorは
angular
やwebapp
など、それぞれ存在するので個別にインストールする
$ sudo npm install -g generator-webapp
インストールの詳細については下記を参考にさせてもらいましたm(__)m
http://dev.classmethod.jp/etc/yeoman/
プロジェクトテンプレートを生成する
プロジェクト用のディレクトリを作成 & 移動
$ mkdir $DIR && cd $DIR
テンプレート生成実行
$ yo webapp
実行後のディレクトリ構成
$ ls -1a . .. .bowerrc .editorconfig .gitattributes .gitignore .jshintrc .yo-rc.json Gruntfile.js app bower.json bower_components node_modules package.json test
Gruntでサーバを立ち上げて確認する
$ grunt server
するとhttp://localhost:9000/
でブラウザが立ち上がる
表示されているページは
app/index.html
試しにapp/index.html
を編集すると、自動的に更新は反映される
livereload
という機能らしい(すげー)
Luceneめも
Luceneの使い方めも
試したLuceneのバージョンは4.0.0
build.gradle
インデックスの作成
IndexWriter
にDocument
を追加していく
インデックスの検索
インデックスを検索するためにQuery
を使う
Queryはいろいろある
NumericRangeQuery
- 数値フィールドの検索に使う
- 1 ~ 10までとか、検索範囲の絞りこみができる
- 最小値と最大値を検索結果に含めるかを指定できる
WildcardQuery
- ワイルドカード使える
TermQuery
- Termを検索
MatchAllDocsQuery
- 全件取得
BooleanQuery
- 条件一致検索
インデックスの更新
- インデックス更新時は、
IndexSearcher
をリフレッシュする
参考
- http://www.techscore.com/tech/Java/ApacheJakarta/Lucene/index/
- http://lucene.apache.org/core/4_10_2/index.html
- http://www.ibm.com/developerworks/jp/opensource/library/os-apache-lucenesearch/
- https://github.com/yamashiro0110/LuceneTest
次はSolrいってみたいな〜
synxでエラーが発生したときのめも
synxを実行しようとしたところ、エラーが発生したのでそのときに対応したこと
原因は、たぶんMacOSのアップデート
10.9 -> 10.10にアップデート
synxでプロジェクト内のディレクトリ構造を同期しようと、以下のコマンドを実行したところエラーが発生
xxx.xcodeprojのあるディレクトリで実行
$ synx xxx.xcodeproj/
ヘルプを表示しようとしてもダメ
$ synx -h /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- xcodeproj/prebuilt/universal.x86_64-darwin14-2.0.0/xcodeproj_ext (LoadError) from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Library/Ruby/Gems/2.0.0/gems/xcodeproj-0.17.0/lib/xcodeproj/ext.rb:6:in `rescue in <top (required)>' from /Library/Ruby/Gems/2.0.0/gems/xcodeproj-0.17.0/lib/xcodeproj/ext.rb:3:in `<top (required)>' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Library/Ruby/Gems/2.0.0/gems/xcodeproj-0.17.0/lib/xcodeproj.rb:30:in `<top (required)>' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Library/Ruby/Gems/2.0.0/gems/synx-0.0.52/lib/synx/project.rb:2:in `<top (required)>' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Library/Ruby/Gems/2.0.0/gems/synx-0.0.52/lib/synx.rb:2:in `<top (required)>' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Library/Ruby/Gems/2.0.0/gems/synx-0.0.52/bin/synx:4:in `<top (required)>' from /usr/bin/synx:23:in `load' from /usr/bin/synx:23:in `<main>'
対応
ライブラリをアップデート
gem
のアップデートを実行
$ sudo gem update --system
xcodeproj
のアップデート
$ sudo gem update xcodeproj
synx
のアップデートsynx
の依存ライブラリにxcodeproj
が含まれてるので、この場合xcodeproj
のアップデートは不要だったかも・・・
$ sudo gem update synx
cocoapodsでも同様のエラーが発生したので、アップデートすると解決した
このコマンドを実行すると
Usage
が表示されるが、同様に上記のエラーが発生
$ pod
- cocoapodsのアップデート
$ sudo gem update pod
参考
update mac osx to 10.10 and pod cannot work · Issue #2219 · CocoaPods/CocoaPods · GitHub
Written with StackEdit.
Gradleでプロジェクトテンプレートを作成
Gradleでプロジェクト作成時に、ディレクトリも一緒に作成する方法のめも
環境
$ gradle -v ------------------------------------------------------------ Gradle 2.1 ------------------------------------------------------------ Build time: 2014-09-08 10:40:39 UTC Build number: none Revision: e6cf70745ac11fa943e19294d19a2c527a669a53 Groovy: 2.3.6 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.7.0_51 (Oracle Corporation 24.51-b03) OS: Mac OS X 10.10 x86_64
プラグインを使う(^o^)
- Gradle Templates Plugin
適当なディレクトリに移動後、下記の内容でbuild.gradle
を作成
今回は
/Users/USER_NAME/workspace/gradle
ディレクトリ内で作成
buildscript { repositories { maven { url 'http://dl.bintray.com/cjstehno/public' } } dependencies { classpath 'gradle-templates:gradle-templates:1.5' } } apply plugin:'templates'
タスクを確認
$ gradle tasks :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- components - Displays the components produced by root project 'java'. dependencies - Displays all dependencies declared in root project 'java'. dependencyInsight - Displays the insight into a specific dependency in root project 'java'. help - Displays a help message projects - Displays the sub-projects of root project 'java'. properties - Displays the properties of root project 'java'. tasks - Displays the tasks runnable from root project 'java'. Template tasks -------------- createGradlePlugin - Creates a new Gradle Plugin project in a new directory named after your project. createGroovyClass - Creates a new Groovy class in the current project. createGroovyProject - Creates a new Gradle Groovy project in a new directory named after your project. createJavaClass - Creates a new Java class in the current project. createJavaProject - Creates a new Gradle Java project in a new directory named after your project. createScalaClass - Creates a new Scala class in the current project. createScalaObject - Creates a new Scala object in the current project. createScalaProject - Creates a new Gradle Scala project in a new directory named after your project. createWebappProject - Creates a new Gradle Webapp project in a new directory named after your project. exportAllTemplates - Exports all the default template files into the current directory. exportGroovyTemplates - Exports the default groovy template files into the current directory. exportJavaTemplates - Exports the default java template files into the current directory. exportPluginTemplates - Exports the default plugin template files into the current directory. exportScalaTemplates - Exports the default scala template files into the current directory. exportWebappTemplates - Exports the default webapp template files into the current directory. initGradlePlugin - Initializes a new Gradle Plugin project in the current directory. initGroovyProject - Initializes a new Gradle Groovy project in the current directory. initJavaProject - Initializes a new Gradle Java project in the current directory. initScalaProject - Initializes a new Gradle Scala project in the current directory. initWebappProject - Initializes a new Gradle Webapp project in the current directory. To see all tasks and more detail, run with --all. BUILD SUCCESSFUL Total time: 4.414 secs
今回はsample_webapp
という名前でWebapp
プロジェクトを作成
プロジェクトは'/Users/USER_NAME/workspace/gradle'以下に作成するものとする
$ gradle createWebappProject > Building 0% > :createWebappProject templates> Project Name: sample_webapp # プロジェクト名を入力 templates> Project Parent Directory: [/Users/USER_NAME/workspace/gradle] # プロジェクトの親ディレクトリ templates> Use Jetty Plugin? (Y|n) [n] # Jetty Pluginを利用するか(そのまんま・・・) templates> Group: [sample_webapp] # グループ名?を設定 templates> Version: [0.1] # プロジェクトのバージョン :createWebappProject BUILD SUCCESSFUL Total time: 28.047 secs
作成されたディレクトリ構造はこんな感じ
$ ls -1R sample_webapp LICENSE.txt build.gradle gradle.properties src sample_webapp/src: main test sample_webapp/src/main: java resources webapp sample_webapp/src/main/java: sample_webapp/src/main/resources: sample_webapp/src/main/webapp: WEB-INF sample_webapp/src/main/webapp/WEB-INF: web.xml sample_webapp/src/test: java resources sample_webapp/src/test/java: sample_webapp/src/test/resources:
プロジェクト用のディレクトリが作成されてる(^o^) gradleのデフォルトのタスクには含まれてないんですかね。。
Written with StackEdit.