{"id":2431,"date":"2021-01-08T15:11:17","date_gmt":"2021-01-08T06:11:17","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=2431"},"modified":"2021-01-08T15:11:17","modified_gmt":"2021-01-08T06:11:17","slug":"post-2431","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2021\/01\/08\/post-2431\/","title":{"rendered":"Laravel\uff1a\u30ab\u30f3\u30de\u306e\u4ed8\u3044\u305f\u6570\u5b57\u3092\u542b\u3080CSV\u30c7\u30fc\u30bf\u3092MySQL\u306bConvert\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<h2>Laravel\uff1a\u30ab\u30f3\u30de\u306e\u4ed8\u3044\u305f\u6570\u5b57\u3092\u542b\u3080CSV\u30c7\u30fc\u30bf\u3092MySQL\u306bConvert\u3059\u308b\u65b9\u6cd5<\/h2>\n<p>\u7d0d\u54c1\u3057\u305f\u30b7\u30b9\u30c6\u30e0\u3067\u304a\u304b\u3057\u306a\u52d5\u304d\u304c\u898b\u3064\u304b\u308a\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u6700\u521d\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5074\u3070\u304b\u308a\u898b\u3066\u3044\u305f\u306e\u3067\u539f\u56e0\u7279\u5b9a\u3067\u304d\u305a\u9014\u65b9\u306b\u66ae\u308c\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u3075\u3068CSV\u30c7\u30fc\u30bf\u306e\u65b9\u306b\u76ee\u3092\u5411\u3051\u305f\u3089\u300c\u6570\u5b57\u306b\u30ab\u30f3\u30de\u304c\u5165\u3063\u3066\u308b\u300d\u3068\u6c17\u3065\u304d\u5373\u4fee\u6b63\u3067\u3059\u3002<\/p>\n<h3>\u307e\u305a\u306fCSV\u30c7\u30fc\u30bf\u306e\u304a\u3055\u3089\u3044<\/h3>\n<p>\u3010 1 \u3011\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u3063\u3066\u3044\u308b\u30bf\u30a4\u30d7<\/p>\n<pre class=\"lang:default decode:true\">\"id\",\"name\",\"price\",\"stock\"\r\n\"1\",\"hoge\",\"700\",\"30\"\r\n\"2\",\"fuga\",\"1000\",\"4000\"\r\n\"3\",\"hega\",\"2,100\",\"200\"<\/pre>\n<p>\u3010 2 \u3011\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u3063\u3066\u3044\u306a\u3044\u30bf\u30a4\u30d7<\/p>\n<pre class=\"lang:default decode:true\">id,name,price,stock\r\n1,hoge,700,30\r\n2,fuga,1000,4000\r\n3,hoga,2100,200<\/pre>\n<p>\u3010 3 \u3011\u4e00\u90e8\u3092\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u3046\u30bf\u30a4\u30d7<\/p>\n<pre class=\"lang:default decode:true\">id,name,price,stock\r\n1,hoge,700,30\r\n2,fuga,\"1,000\",\"4,000\"\r\n3,hoga,\"2,100\",200<\/pre>\n<p>\u4eca\u56de\u306e\u30a8\u30e9\u30fc\u3092\u5f15\u304d\u8d77\u3053\u3057\u3066\u3044\u305f\u30c7\u30fc\u30bf\u306f\u30103\u3011\u306e\u30bf\u30a4\u30d7\u3067\u3057\u305f\u3002<br \/>\nExcel\u3067\u4f5c\u6210\u3057\u305f\u30c7\u30fc\u30bf\u306f\u30ab\u30f3\u30de\u306e\u6709\u7121\u3067\u30102\u3011\u304b\u30103\u3011\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>Laravel\u304b\u3089MySQL\u3078\u683c\u7d0d\u3057\u3088\u3046\u3068\u3057\u305f\u6642\u3001INT\u306e\u898f\u5236\u306b\u639b\u304b\u3063\u3066\u3057\u307e\u3046\u306e\u306f\u30101\u3011\u3068\u30103\u3011\u3067\u3059\u3002<br \/>\n\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u3046\uff1d\u6587\u5b57\u5217\u306a\u306e\u3067\u300c\u6570\u5024\u6307\u5b9a\u3057\u3066\u308b\u306e\u306b\u4f55\u5165\u308c\u3066\u3093\u3060\u300d\u3068\u306a\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u4ee5\u524d\u306e\u30c7\u30fc\u30bf\u306f\u30101\u3011\u306e\u30bf\u30a4\u30d7\u3060\u3063\u305f\u305f\u3081\u3001\u3053\u306e\u56de\u907f\u306b (int)$row[3] \u3068\u66f8\u3044\u3066\u3044\u3044\u307e\u3057\u305f\u3002<br \/>\n\u3053\u308c\u304c\u30103\u3011\u306e\u30bf\u30a4\u30d7\u306b\u5909\u308f\u308a\u30c7\u30fc\u30bf\u4e0d\u6574\u5408\u3092\u5f15\u304d\u8d77\u3053\u3057\u3066\u3044\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<h3>\u30ab\u30f3\u30de\u533a\u5207\u308a\u6570\u5b57\u306e\u3042\u308bCSV\u30c7\u30fc\u30bf\u306e\u8aad\u8fbc\u307f\u65b9\u6cd5 \uff5e\u305d\u306e1\uff5e<\/h3>\n<p>\u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u6570\u5b57\u3092\u542b\u3080CSV\u306e\u51e6\u7406\u306f\u3001Data\u7dcf\u6570\u304c\u5c11\u306a\u3044\u5834\u5408 str_getcsv\u95a2\u6570\u304c\u5feb\u9069\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">test.csv\r\n---------\r\nid,name,price,stock\r\n1,hoge,700,30\r\n2,fuga,\"1,000\",\"4,000\"\r\n3,hoga,\"2,100\",200\r\n----------\r\n<\/pre>\n<pre class=\"lang:default decode:true\">$input = fopen(\"test.csv\", \"r\");\r\n\r\n  \/\/1\u884c\u3054\u3068\u8aad\u3080\r\n  while($result= str_getcsv($input)){\r\n    print_r($result);\r\n  }\r\nfclose($f);<\/pre>\n<pre class=\"lang:default decode:true\">Array\r\n(\r\n    [0] =&gt; 'id'\r\n    [1] =&gt; 'name'\r\n    [2] =&gt; 'price'\r\n    [3] =&gt; 'stock'\r\n)\r\nArray\r\n(\r\n    [0] =&gt; '1'\r\n    [1] =&gt; 'hoge'\r\n    [2] =&gt; '700'\r\n    [3] =&gt; '30'\r\n)\r\nArray\r\n(\r\n    [0] =&gt; '2'\r\n    [1] =&gt; 'fuga'\r\n    [2] =&gt; '1,000'\r\n    [3] =&gt; '4,000'\r\n)\r\nArray\r\n(\r\n    [0] =&gt; '3'\r\n    [1] =&gt; 'hoga'\r\n    [2] =&gt; '2,100'\r\n    [3] =&gt; '200'\r\n)<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3057\u3063\u304b\u308a\u3068\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u56f2\u307e\u308c\u305f\u30ab\u30f3\u30de\u3092\u6587\u5b57\u5217\u3068\u3057\u3066\u8a8d\u8b58\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h4>str_getcsv\u3067\u56de\u907f\u3057\u305f\u5834\u5408\u306e\u554f\u984c\u70b9<\/h4>\n<p>str_getcsv\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3001fopen\u306a\u3069\u3067\u8aad\u307f\u8fbc\u3093\u3067\u884c\u3054\u3068\u306b\u51e6\u7406\u3059\u308b\u3068\u8a00\u3046\u6d41\u308c\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u305d\u306e\u70ba\u3001\u30b5\u30a4\u30ba\u306e\u5927\u304d\u306aCSV\u306e\u5834\u5408\u306a\u3069\u30673\u3064\u306e\u5927\u304d\u306a\u8ab2\u984c\u3092\u62b1\u3048\u308b\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"paperBox\">\n<ol>\n<li>\u52d5\u4f5c\u304c\u9045\u3044<\/li>\n<li>\u30e1\u30e2\u30ea\u3092\u98df\u3046<\/li>\n<li>\u30e1\u30e2\u30ea\u4e0d\u8db3\u3067\u30a8\u30e9\u30fc\u3067\u5b8c\u8d70\u3067\u304d\u306a\u3044<\/li>\n<\/ol>\n<\/div>\n<h3>\u30ab\u30f3\u30de\u533a\u5207\u308a\u6570\u5b57\u306e\u3042\u308bCSV\u30c7\u30fc\u30bf\u306e\u8aad\u8fbc\u307f\u65b9\u6cd5 \uff5e\u305d\u306e2\uff5e<\/h3>\n<p>\u500b\u4eba\u7684\u306b\u306f str_getcsv\u95a2\u6570\u3088\u308a\u3082\u3053\u3061\u3089\u306e\u65b9\u304c\u304a\u52e7\u3081\u3067\u3059\u3002<\/p>\n<div class=\"borderBox\">\n<p>SplFileObject\u95a2\u6570\u3067CSV\u3092\u8aad\u8fbc\u3080<\/p>\n<\/div>\n<pre class=\"lang:default decode:true\">\/\/ setlocale\u3092\u8a2d\u5b9a\r\nsetlocale(LC_ALL, 'ja_JP.UTF-8');\r\nfile_path = \"..\/test.csv\";\r\n\r\n$file = new SplFileObject($file_path);\r\n$file-&gt;setFlags(\\SplFileObject::READ_CSV);\r\n\r\n$row_count = 1;\/\/\u521d\u671f\u30ab\u30a6\u30f3\u30c8\u7528\u610f\r\n\r\n#\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305fCSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u8fbc\r\nforeach ($file as $row){\r\n  if ($row === [null]) continue;\/\/\u6700\u7d42\u884c\u306e\u51e6\u7406(\u6700\u7d42\u884c\u304c\u7a7a\u3063\u307d\u306e\u5834\u5408\u306e\u5bfe\u7b56)\r\n  if ($row_count &gt; 1){\/\/2\u884c\u76ee\u304b\u3089\u30b9\u30bf\u30fc\u30c8\r\n    mb_convert_variables('UTF-8', 'SJIS-WIN', $row);\r\n    print_r($row);\r\n  }\r\n  $row_count++;  \r\n}<\/pre>\n<pre class=\"lang:default decode:true\">Array\r\n(\r\n    [0] =&gt; '1'\r\n    [1] =&gt; 'hoge'\r\n    [2] =&gt; '700'\r\n    [3] =&gt; '30'\r\n)\r\nArray\r\n(\r\n    [0] =&gt; '2'\r\n    [1] =&gt; 'fuga'\r\n    [2] =&gt; '1,000'\r\n    [3] =&gt; '4,000'\r\n)\r\nArray\r\n(\r\n    [0] =&gt; '3'\r\n    [1] =&gt; 'hoga'\r\n    [2] =&gt; '2,100'\r\n    [3] =&gt; '200'\r\n)<\/pre>\n<p>foreach\u5185\u30671\u884c\u76ee\u306e\u51fa\u529b\uff08print_r\uff09\u3092\u98db\u3070\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u3053\u3093\u306a\u611f\u3058\u3067\u304a\u99b4\u67d3\u307f\u306eforeach\u5185\u3067\u6a19\u6e96\u7684\u306a\u914d\u5217\u3068\u3057\u3066\u6271\u3048\u308b\u306e\u3067\u597d\u304d\u306a\u3088\u3046\u306b\u5f04\u308c\u307e\u3059\u3002<\/p>\n<h3>\u914d\u5217\u306e\u6570\u304c\u554f\u984c\u306a\u3044\u306e\u306b\u767b\u9332\u3059\u308b\u5024\u304c\u30aa\u30ab\u30b7\u30a4<\/h3>\n<p>\u4eca\u56de\u767a\u751f\u3057\u3066\u3044\u305f\u554f\u984c\u300e1,000 \u3068\u3044\u3046\u6570\u5b57\u3092SQL\u306b\u683c\u7d0d\u3057\u305f\u3089 1 \u304c\u767b\u9332\u3055\u308c\u3066\u3044\u305f\u300f\u3068\u3044\u3046\u3082\u306e\u3067\u3057\u305f\u3002<\/p>\n<p>\u3082\u3068\u3082\u3068\u3010\u305d\u306e2\u3011\u306e\u65b9\u6cd5\u3067\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u7d44\u3093\u3067\u3044\u305f\u306e\u3067\u3001\u914d\u5217\u306e\u6570\u306b\u554f\u984c\u306f\u306a\u304f\u3010 [2] =&gt; '1,000' \u3011\u3068\u3057\u3066\u683c\u7d0d\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u306b (int)$row[2] \u3068\u3057\u3066\u6570\u5024\u5316\u3057\u3066\u683c\u7d0d\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u3053\u306e\u6bb5\u968e\u3067\u30ab\u30f3\u30de\u306e\u524d\u3060\u3051\u304c\u53d6\u308a\u51fa\u3055\u308c\u3066\u3044\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u5b9f\u9a13\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true\">\/\/ setlocale\u3092\u8a2d\u5b9a\r\nsetlocale(LC_ALL, 'ja_JP.UTF-8');\r\nfile_path = \"..\/test.csv\";\r\n\r\n$file = new SplFileObject($file_path);\r\n$file-&gt;setFlags(\\SplFileObject::READ_CSV);\r\n\r\n$row_count = 1;\/\/\u521d\u671f\u30ab\u30a6\u30f3\u30c8\u7528\u610f\r\n\r\n#\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305fCSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u8fbc\r\nforeach ($file as $row){\r\n  if ($row === [null]) continue;\/\/\u6700\u7d42\u884c\u306e\u51e6\u7406(\u6700\u7d42\u884c\u304c\u7a7a\u3063\u307d\u306e\u5834\u5408\u306e\u5bfe\u7b56)\r\n  if ($row_count &gt; 1){\/\/2\u884c\u76ee\u304b\u3089\u30b9\u30bf\u30fc\u30c8\r\n    mb_convert_variables('UTF-8', 'SJIS-WIN', $row);\r\n    print_r('1\uff1a'.$row[2]);\r\n    print_r('2\uff1a'.(int)$row[2]);\r\n    print_r('3\uff1a'.str_replace(\",\",\"\",$row[2]));\r\n    print_r('4\uff1a'.(int)str_replace(\",\",\"\",$row[2]));\r\n  }\r\n  $row_count++;  \r\n}<\/pre>\n<pre class=\"lang:default decode:true\">#2\u30ec\u30b3\u30fc\u30c9\u76ee\u306e\u307f\u8a18\u8f09\r\n\r\n1\uff1a'1,000'\r\n2\uff1a1\r\n3\uff1a'1000'\r\n4\uff1a1000<\/pre>\n<p>\u30ab\u30f3\u30de\u304c\u5165\u3063\u305f\u6570\u5024\u3092(int)\u3067\u6570\u5024\u5909\u63db\u3057\u3066\u3042\u3052\u308b\u3068\u30ab\u30f3\u30de\u306e\u524d\u3060\u3051\u53d6\u308a\u51fa\u3055\u308c\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3092\u51e6\u7406\u3059\u308b\u306b\u306f\u300c\u30ab\u30f3\u30de\u3092\u5f37\u5236\u7684\u306b\u53d6\u3063\u3066\u3042\u3052\u308c\u3070\u3088\u3044\u300d\u3068\u3044\u3046\u3053\u3068\u3067\u3001Replace\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u305d\u306e\u7d50\u679c\u3001SQL\u306e\u683c\u7d0d\u3082\u60f3\u5b9a\u901a\u308a\u306b\u52d5\u4f5c\u3002<\/p>\n<pre class=\"lang:default decode:true\">if(!empty($row)){\r\n  #\u65b0\u898f\u30ec\u30b3\u30fc\u30c9\u8ffd\u52a0\r\n  $item = new Temporaryfile();\r\n  $item-&gt;name = $row[1];\r\n  $item-&gt;price= $row[2];\r\n  $item-&gt;stock = $row[3];\r\n  $item-&gt;save();\r\n}<\/pre>\n<h3>\u307e\u3068\u3081<\/h3>\n<div class=\"borderBox\">\n<p>$row[2] = \"1,234\" \u306e\u6642\u3001(int)str_replace(\",\",\"\",$row[2]));\u3000\u3068\u3059\u308b\u3068\u30001234 \u3092\u53d6\u308a\u51fa\u305b\u308b\u3002<\/p>\n<\/div>\n<p>\u4eca\u56de\u306f\u6700\u521d\u306e\u539f\u56e0\u7a76\u660e\u306b\u6642\u9593\u3092\u53d6\u3089\u308c\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u304c\u3001\u305d\u3053\u304c\u7d42\u308f\u308c\u3070\u3042\u3063\u3055\u308a\u89e3\u6c7a\u306b\u81f3\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3068\u3044\u3046\u304b\u306d\u3001\u6700\u8fd1\u306fVBScript\u3068Python\u3070\u304b\u308a\u3067PHP\u89e6\u3063\u3066\u306a\u304b\u3063\u305f\u306e\u30672\u6642\u9593\u307b\u3069\u982d\u306e\u611f\u899a\u304c\u623b\u3089\u306a\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u6e80\u904d\u306a\u304f\u89e3\u6c7a\u65e9\u3044\u4eba\u3063\u3066\u3044\u307e\u3059\u3051\u3069\u3001\u5c0a\u656c\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel\uff1a\u30ab\u30f3\u30de\u306e\u4ed8\u3044\u305f\u6570\u5b57\u3092\u542b\u3080CSV\u30c7\u30fc\u30bf\u3092MySQL\u306bConvert\u3059\u308b\u65b9\u6cd5 \u7d0d\u54c1\u3057\u305f\u30b7\u30b9\u30c6\u30e0\u3067\u304a\u304b\u3057\u306a\u52d5\u304d\u304c\u898b\u3064\u304b\u308a\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002 \u6700\u521d\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u5074\u3070\u304b\u308a\u898b\u3066\u3044\u305f\u306e\u3067\u539f\u56e0\u7279\u5b9a\u3067\u304d\u305a\u9014\u65b9\u306b\u66ae\u308c\u3066\u3044\u305f\u306e\u3067\u3059\u304c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":944,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[34],"tags":[19,153,99,98,97,53,45],"class_list":["post-2431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-memorandum","tag-ec","tag-laravel","tag-mysql","tag-php","tag-97","tag-53","tag-45"],"veu_head_title_object":{"title":"","add_site_title":""},"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2018\/11\/laravel_logo.png","_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/2431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=2431"}],"version-history":[{"count":0,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/2431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/944"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=2431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=2431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=2431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}