おつかれさまです。
cakePHP バージョン3を使用してプログラミングしているのですが、SJISで指定しているのにもかかわらず、エクセルでCSVデータを表示させようとしたところ、文字化けしてしまっているということと、データが自分の思うようには格納されていません。
コード内容は下記になっております
PHP
1public function goldList() { 2 //CSV出力のためビューはなし 3 $this->autoRender = false; 4 5 //データ 6 $csv = $this->ProProf->getList(); 7 8 array_walk_recursive( $csv, function( &$d, $key ) { // 配列の値を参照として渡している( &$d ) 9 //両端の空白文字の削除 10 $d = trim( $d ); 11 12 //先頭の文字列の削除 13 $d = ltrim( $d, "-" ); 14 $d = ltrim( $d, "=" ); 15 16 //削除する文字列 17 $search = array( ",", '"', "\r\n" ); 18 $d = str_replace( $search, "", $d ); 19 //文字コードの変換 20 mb_convert_variables('SJIS', 'UTF-8', $d); 21 }); 22 23 $this->response->type('csv'); 24 $this->response->body($csv); 25 }
なお$csv内容は下記になります。
PHP
1array(101) { 2 [0]=> 3 array(20) { 4 ["id"]=> 5 string(1) "2" 6 ["usr_id"]=> 7 string(5) "10640" 8 ["name"]=> 9 string(12) "masacorleone" 10 ["sex"]=> 11 string(6) "男性" 12 ["birth"]=> 13 string(10) "0000-00-00" 14 ["area"]=> 15 string(12) "神奈川県" 16 ["mail"]=> 17 string(21) "negishi0094@gmail.com" 18 ["job"]=> 19 string(0) "" 20 ["jnre"]=> 21 string(0) "" 22 ["prof"]=> 23 string(0) "" 24 ["level"]=> 25 string(0) "" 26 ["history"]=> 27 string(0) "" 28 ["job_jnre"]=> 29 string(0) "" 30 ["job_history1"]=> 31 string(0) "" 32 ["job_history2"]=> 33 string(0) "" 34 ["job_history3"]=> 35 string(0) "" 36 ["others"]=> 37 string(0) "" 38 ["pr"]=> 39 string(0) "" 40 ["post_at"]=> 41 string(19) "2014-08-21 18:07:11" 42 ["del_flg"]=> 43 string(1) "0" 44 } 45 [1]=> 46 array(20) { 47 ["id"]=> 48 string(1) "3" 49 ["usr_id"]=> 50 string(6) "127153" 51 ["name"]=> 52 string(11) "moshiko0910" 53 ["sex"]=> 54 string(6) "女性" 55 ["birth"]=> 56 string(10) "1986-09-10" 57 ["area"]=> 58 string(9) "愛知県" 59 ["mail"]=> 60 string(21) "iroas0910@ezweb.ne.jp" 61 ["job"]=> 62 string(0) "" 63 ["jnre"]=> 64 string(6) "美容" 65 ["prof"]=> 66 string(74) "コスメがすごい好きで、とにかく試してます(*^ー^)ノ♪" 67 ["level"]=> 68 string(12) "シルバー" 69 ["history"]=> 70 string(10) "1年未満" 71 ["job_jnre"]=> 72 string(21) "口コミ・体験談" 73 ["job_history1"]=> 74 string(10) "`` `` ``" 75 ["job_history2"]=> 76 string(10) "`` `` ``" 77 ["job_history3"]=> 78 string(10) "`` `` ``" 79 ["others"]=> 80 string(13) "医療 `` ``" 81 ["pr"]=> 82 string(0) "" 83 ["post_at"]=> 84 string(19) "2014-08-21 18:53:52" 85 ["del_flg"]=> 86 string(1) "0" 87 }
理想としては
1列目横一列(20行)をkeyの値を入れにし、
2列目以降は該当するデータを格納して、csvデータできちんと表示されるようにしたいです。
お手数おかけしますがお力添えの程宜しくお願い申し上げます。。。
デスクトップに入れたファイルの拡張子を.csvにしてエクセルで開いたデータは下記の通りです
csv
1<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr570a731d918c9-trace').style.display = (document.getElementById('cakeErr570a731d918c9-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Missing argument 1 for App\Controller\Component\ProProfComponent::getList() called in /var/www/html/adm.repo.ne.jp/src/Controller/UtilController.php on line 33 and defined [<b>APP/Controller/Component/ProProfComponent.php</b> line <b>18</b>]<div id="cakeErr570a731d918c9-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr570a731d918c9-code').style.display = (document.getElementById('cakeErr570a731d918c9-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr570a731d918c9-context').style.display = (document.getElementById('cakeErr570a731d918c9-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr570a731d918c9-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">* @return array / </span><span style="color: #0000BB">false</span></span></code> 2<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">*/</span></span></code></span> 3<code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">public function </span><span style="color: #0000BB">getList</span><span style="color: #007700">(</span><span style="color: #0000BB">$logId</span><span style="color: #007700"> </span><span style="color: #0000BB">$query</span><span style="color: #007700">=[]) {</span></span></code></pre><pre class="stack-trace">App\Controller\Component\ProProfComponent::getList() - APP/Controller/Component/ProProfComponent.php line 18 4App\Controller\UtilController::goldList() - APP/Controller/UtilController.php line 33 5Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php line 429 6Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php line 114 7Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php line 87 8[main] - ROOT/webroot/index.php line 36</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr570a731da1ea4-trace').style.display = (document.getElementById('cakeErr570a731da1ea4-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Array to string conversion [<b>CORE/src/Network/Response.php</b> line <b>559</b>]<div id="cakeErr570a731da1ea4-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr570a731da1ea4-code').style.display = (document.getElementById('cakeErr570a731da1ea4-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr570a731da1ea4-context').style.display = (document.getElementById('cakeErr570a731da1ea4-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr570a731da1ea4-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">}</span></span></code> 9<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"></span></span></code></span> 10<code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">echo </span><span style="color: #0000BB">$content</span><span style="color: #007700">;</span></span></code></pre><pre id="cakeErr570a731da1ea4-context" class="cake-context" style="display: none;">$content = [ 11" (int) 0 => [" 12" 'id' => '2'" 13" 'usr_id' => '10640'" 14" 'name' => 'masacorleone'" 15" 'sex' => '・スj・ス・ス'" 16" 'birth' => '0000-00-00'" 17" 'area' => '・ス_・ズ川県'" 18" 'mail' => 'negishi0094@gmail.com'" 19" 'job' => ''" 20" 'jnre' => ''" 21" 'prof' => ''" 22" 'level' => ''" 23" 'history' => ''" 24" 'job_jnre' => ''" 25" 'job_history1' => ''" 26" 'job_history2' => ''" 27" 'job_history3' => ''" 28" 'others' => ''" 29" 'pr' => ''" 30" 'post_at' => '2014-08-21 18:07:11'" 31" 'del_flg' => '0'" 32" ]" 33" (int) 1 => [" 34" 'id' => '3'" 35" 'usr_id' => '127153'" 36" 'name' => 'moshiko0910'" 37" 'sex' => '・ス・ス・ス・ス'" 38" 'birth' => '1986-09-10'" 39" 'area' => '・ス・ス・スm・ス・ス'" 40" 'mail' => 'iroas0910@ezweb.ne.jp'" 41" 'job' => ''" 42" 'jnre' => '・ス・ス・スe'" 43" 'prof' => '・スR・スX・ス・ス・ス・ス・ス・ス・ス・ス・ス・ス・スD・ス・ス・スナ、・スニにゑソス・ス・ス・ス・ス・ス・ス・ストまゑソス(*^・ス[^)・スm・ス・ス'" 44" 'level' => '・スV・ス・ス・スo・ス['" 45" 'history' => '1・スN・ス・ス・ス・ス'" 46" 'job_jnre' => '・ス・ス・スR・ス~・スE・スフ鯉ソス・スk'" 47" 'job_history1' => '`` `` ``'" 48" 'job_history2' => '`` `` ``'" 49" 'job_history3' => '`` `` ``'" 50" 'others' => '・ス・ス・ス `` ``'" 51" 'pr' => ''" 52" 'post_at' => '2014-08-21 18:53:52'" 53" 'del_flg' => '0'" 54" ]" 55
よろしくお願いします