PHP:CSV到数组将值存储到数组中,在print_r中显示,访问时为空

时间:2019-06-28 19:41:48

标签: php wordpress csv import

我的任务是使用旧站点数据更新新站点。因此,将csv导入自定义帖子类型。

我正在执行导入操作,但不幸的是,javascript方面在包含html的csv列方面表现不佳。

为解决此问题,我发现我的解析函数在html列中没有任何问题。现在错误发生了,我数组的第一个键没有保留其数据。以前从未见过,有点心动。让我知道您是否曾经看过此问题以及如何解决。它也发生在每一行,不会返回artkey。

首先是我的wordpress / php代码,用于解析csv

function waw_article_csv_to_array($filename='', $delimiter=',')
  {

      // this is an important setting!
     ini_set('auto_detect_line_endings', true);


  // IF the file doesn't exist or the file is not readable return false
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    // setup our return data  
    $return_data = array();

    // IF we can open and read the file
    if (($handle = fopen($filename, "r")) !== FALSE) {

      $row = 0;

      // while data exists loop over data
      while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
          // count the number of items in this data
          $num = count($data);
          // loop over all items and append them to our row data
          for ($c=0; $c < $num; $c++) {
              // if this is the first row set it up as our header
              if( $row == 0):
                $header[] = $data[$c];
              else:
                  // all rows greater than 1
                  //add to $return_data
                  $return_data[$row-1][$header[$c]] = $data[$c];
              endif;
          }
          $row++;
      } 

      // close our file
      fclose($handle);
  }
  // Log Testing
    error_log('Array of Keys'. print_r( array_keys($return_data[30]),true));
    error_log('Return Data Array: '. print_r($return_data[30],true));

    error_log('artkey:'. $return_data[30]['artkey']);
    error_log('Arttitle:'.$return_data[30]['arttitle']);
    error_log('issue:'.$return_data[30]['issue']);
    error_log('year:'.$return_data[30]['year']);
    error_log('teaser:'.$return_data[30]['teaser']);
    error_log('story:'.$return_data[30]['story']);
    error_log('images:'.$return_data[30]['images']);
    error_log('author:'.$return_data[30]['author']);
    error_log('publish:'.$return_data[30]['publish']);
    error_log('sidebardisplay:'.$return_data[30]['sidebardisplay']);
    error_log('notes:'.$return_data[30]['notes']);
    error_log('region:'.$return_data[30]['region']);
    error_log('cats:'.$return_data[30]['cats']);
error_log('Return Data Array[28]: '. print_r($return_data[28],true));
error_log('artkey[28]:'. $return_data[28]['artkey']);    
error_log('Return Data Array[29]: '. print_r($return_data[29],true));
error_log('artkey[29]:'. $return_data[29]['artkey']);
error_log('Return Data Array[30]: '. print_r($return_data[30],true));
error_log('artkey[30]:'. $return_data[30]['artkey']);
error_log('Var Dump 30:'.var_dump($return_data[30]));



    return $return_data;
}

在我的日志测试过程中发现了问题,我只检查了我选择的任何行,在此示例中是第30行。当我访问“ artkey”时,它返回空或空白。

下面是数组键,以及数组的print_r以及返回的日志值。

[28-Jun-2019 19:14:33 UTC] Array of KeysArray
(
    [0] => artkey
    [1] => arttitle
    [2] => issue
    [3] => year
    [4] => teaser
    [5] => story
    [6] => images
    [7] => author
    [8] => publish
    [9] => sidebardisplay
    [10] => notes
    [11] => region
    [12] => cats
    [13] => editdate
)

[28-Jun-2019 19:14:33 UTC] Return Data Array: Array
(
    [artkey] => 9c0e34da-e3ff-4d51-ae71-6b7003f40885
    [arttitle] => Pennsylvania Bucket List Challenge
    [issue] => Summer
    [year] => 2019
    [teaser] => <p>Take our Summer Bucket List Challenge</p>
    [story] => <p><img class="fr-dib fr-draggable" src="/_img/articles/PA SUMMER Bucket List Challenge 2019.jpg" style="width: 100%;"></p>
    [images] => 
    [author] => Wendy 
    [publish] => Y
    [sidebardisplay] => N
    [notes] => 
    [region] => North,South,East,West
    [cats] => 
    [editdate] => 0000-00-00 00:00:00
)

[28-Jun-2019 19:14:33 UTC] artkey:
[28-Jun-2019 19:14:33 UTC] Arttitle:Pennsylvania Bucket List Challenge
[28-Jun-2019 19:14:33 UTC] issue:Summer
[28-Jun-2019 19:14:33 UTC] year:2019
[28-Jun-2019 19:14:33 UTC] teaser:<p>Take our Summer Bucket List Challenge</p>
[28-Jun-2019 19:14:33 UTC] story:<p><img class="fr-dib fr-draggable" src="/_img/articles/PA SUMMER Bucket List Challenge 2019.jpg" style="width: 100%;"></p>
[28-Jun-2019 19:14:33 UTC] images:
[28-Jun-2019 19:14:33 UTC] author:Wendy 
[28-Jun-2019 19:14:33 UTC] publish:Y
[28-Jun-2019 19:14:33 UTC] sidebardisplay:N
[28-Jun-2019 19:14:33 UTC] notes:
[28-Jun-2019 19:14:33 UTC] region:North,South,East,West
[28-Jun-2019 19:14:33 UTC] cats:

我不确定我的php数组发生了什么,但是print_r将使用任何索引(除了使用的30)显示artkey的值,但是当我要通过数组键访问它时将无法访问它。感谢任何帮助,就像我写的不确定的6个进口商一样。

按要求返回的其他行的数据

[28-Jun-2019 19:55:53 UTC] Return Data Array[30]: Array
(
    [artkey] => 9c0e34da-e3ff-4d51-ae71-6b7003f40885
    [arttitle] => Pennsylvania Bucket List Challenge
    [issue] => Summer
    [year] => 2019
    [teaser] => <p>Take our Summer Bucket List Challenge</p>
    [story] => <p><img class="fr-dib fr-draggable" src="/_img/articles/PA SUMMER Bucket List Challenge 2019.jpg" style="width: 100%;"></p>
    [images] => 
    [author] => Wendy 
    [publish] => Y
    [sidebardisplay] => N
    [notes] => 
    [region] => North,South,East,West
    [cats] => 
    [editdate] => 0000-00-00 00:00:00
)

[28-Jun-2019 19:55:53 UTC] artkey[30]:
[28-Jun-2019 19:55:53 UTC] Return Data Array[29]: Array
(
    [artkey] => 3dda11a0-37c2-4e87-b5ed-e6b2576f25ee
    [arttitle] => June 2019 Events in Pennsylvania: Historic
    [issue] => Summer
    [year] => 2019
    [teaser] => <p>If you&apos;re into history, you&apos;ll love these PA events</p>
    [story] => <h2 style="text-align: left;"><strong><span style="color: rgb(0, 168, 133); font-size: 24px;">HISTORIC EVENTS</span></strong></h2><h2 style="text-align: right;"><img class="fr-dib fr-draggable" src="/_img/articles/FIshooter.jpg" style="width: 100%;"><span style="font-size: 10px;"><em>Cook Forest French &amp; Indian War Reenactment&nbsp;</em></span></h2><h2 style="text-align: left;"><span style="font-size: 18px;">May 25-Jul 16.&nbsp;</span><strong><span style="font-size: 18px;">We Can Do It! WWII Traveling Exhibit &amp; Events.</span></strong></h2><p style="text-align: left;">Altoona Railroaders Memorial Museum | 1300 Ninth Ave. | Altoona, PA | 814-946-0834<br><a href="https://whereandwhen.com/events/47628/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449153727505693">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 1.&nbsp;</span><strong><span style="font-size: 18px;">Exploring the Armor.</span></strong></h2><p style="text-align: left;">PA Military Museum | 51 Boal Ave. | Boalsburg, PA 16827 | 814-466-6263<br><a href="https://whereandwhen.com/events/48104/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449285435637163">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 1-2.&nbsp;</span><strong><span style="font-size: 18px;">Korean War Traveling Memorial.</span></strong></h2><p style="text-align: left;">Greene County Fairgrounds | 107 Fairgrounds Road | Waynesburg, PA 15370 | 724-627-8687<br><a href="https://whereandwhen.com/events/49188/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449334775601451">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 1.&nbsp;</span><strong><span style="font-size: 18px;">Bookbinding Workshop.</span></strong></h2><p style="text-align: left;">690 Sugartown Road | Malvern, PA 19355 | 610-640-2667<br><a href="https://whereandwhen.com/events/49600/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449388780331772">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 6-8.&nbsp;</span><strong><span style="font-size: 18px;">75th Anniversary of D-Day.</span></strong></h2><p style="text-align: left;">Eldred World War II Museum | 201 Main St. | Eldred, PA 16731 | 814-225-2220<br><a href="https://whereandwhen.com/events/49209/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449445778398824">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 7-9.&nbsp;</span><strong><span style="font-size: 18px;">World War II Weekend.</span></strong></h2><p style="text-align: left;">Mid-Atlantic Air Museum | 11 Museum Drive | Reading, PA 19605 | 610-372-7333</p><p style="text-align: left;"><a href="https://whereandwhen.com/events/49243/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449525249979605">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 8-9.&nbsp;</span><strong><span style="font-size: 18px;">Cook Forest French &amp; Indian War Encampment.</span></strong></h2><p style="text-align: left;">Cook Forest Sawmill Center for the Arts | 140-170 Theater Lane | Cooksburg, PA 16217 | 814-927-6655<br><a href="https://whereandwhen.com/events/49105/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449607889995875">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 16.&nbsp;</span><strong><span style="font-size: 18px;">18th Century Day.</span></strong><br><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;">Brandywine Battlefield Park |1491 Baltimore Pike | Chadds Ford, PA | 610-459-3342<br></span><a href="https://whereandwhen.com/events/48537/"><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449654273444362">detail</gwmw></span></a></h2><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 22.&nbsp;</span><strong><span style="font-size: 18px;">Traditional Cherry Fair &amp; Early American Craft Show.</span></strong></h2><p style="text-align: left;">Alexander Schaeffer Farm |213 S. Carpenter St. | <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638479457839002588">Schaefferstown</gwmw>, PA 17088 | 717-949-2244</p><p style="text-align: left;"><br><a href="https://whereandwhen.com/events/49055/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449701559414876">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 22-23.&nbsp;</span><strong><span style="font-size: 18px;">Wild West Weekend.</span></strong></h2><p style="text-align: left;">Old Bedford Village | 220 Sawblade Road | Bedford, PA 15522 | 814-623-1156<br><a href="https://whereandwhen.com/events/49013/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449773391335356">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 28-30.&nbsp;</span><strong><span style="font-size: 18px;">Living History Encampment &amp; Tactical Demonstrations.</span></strong></h2><p style="text-align: left;"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638486214092116172">Solanco</gwmw> Historical Society | 1932 Robert Fulton Highway | Quarryville, PA<br><a href="https://whereandwhen.com/events/49295/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638449822562246006">detail</gwmw></a></p>
    [images] => 
    [author] => W&W
    [publish] => Y
    [sidebardisplay] => N
    [notes] => 
    [region] => North,South,East,West
    [cats] => fairs-festivals,historical-sites,outdoor-recreation-outdoor-recreation
    [editdate] => 5/21/19 14:37
)

[28-Jun-2019 19:55:53 UTC] artkey[29]:
[28-Jun-2019 19:55:53 UTC] Return Data Array[28]: Array
(
    [artkey] => e55c41b3-d5ce-411a-a33e-48e46a5e55f1
    [arttitle] => June 2019 Events in Pennsylvania: Kids
    [issue] => Summer
    [year] => 2019
    [teaser] => <p>Check out these June events in PA that kids will love!</p>
    [story] => <h2><span style="font-size: 24px;"><strong><span style="color: rgb(243, 121, 52);">June Events That the Kids Will Love!</span></strong></span></h2><p style="text-align: right;"><span style="font-size: 24px;"><strong><span style="color: rgb(243, 121, 52);"><img class="fr-dib fr-draggable" src="/_img/articles/Youth-&amp;-Family-Field-Day.jpg" style="width: 100%;"></span></strong></span><span style="font-size: 11px;"><em class="">Youth &amp; Family Field Day, Pennsylvania Lumber Museum</em></span></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 1-2, 8-9, 15-16.&nbsp;</span><strong><span style="font-size: 18px;">Strawberry Fun Days.</span></strong></h2><p style="text-align: left;">Shady Brook Farm | 931 Stony Hill Road | Yardley, PA 19067 | 215-598-7240</p><p style="text-align: left;"><a href="https://whereandwhen.com/events/49260/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638695672727548523">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 2.&nbsp;</span><strong><span style="font-size: 18px;">Youth Fishing Derby.</span></strong></h2><p style="text-align: left;">Stone Hedge Golf Course | 287 German Hill Road | Factoryville, PA | 570-836-5108<br><a href="https://whereandwhen.com/events/48294/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638695760725380801">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 7-9.&nbsp;</span><strong><span style="font-size: 18px;">Annual Snake Hunt.</span></strong></h2><p style="text-align: left;">15 Club Road | Sinnemahoning, PA 15861 | 814-546-2835<br><a href="https://whereandwhen.com/events/49102/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638695822865132126">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 8.&nbsp;</span><strong><span style="font-size: 18px;">Children&apos;s Day.</span></strong></h2><p style="text-align: left;">Hookstown Fair Grounds, 1198 Route 168 | Hookstown, PA |<br><a href="https://whereandwhen.com/events/48274/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638695872627158580">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 8.&nbsp;</span><strong><span style="font-size: 18px;">Youth &amp; Family Field Day.</span></strong></h2><p style="text-align: left;">PA Lumber Museum | 5660 Route 6 West | Galeton, PA 16922 | 814-435-2652<br><a href="https://whereandwhen.com/events/49525/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638695920597766354">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 9.&nbsp;</span><strong><span style="font-size: 18px;"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638712821698110797">Ecozone</gwmw> Discovery Room.</span></strong></h2><p style="text-align: left;">Pocono Environmental Education Center | 538 Emery Road | Dingmans Ferry, PA 18328 | 570-828-2319<br><a href="https://whereandwhen.com/events/49555/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638695997966007842">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 18-Aug 6.&nbsp;</span><strong><span style="font-size: 18px;">Children&apos;s Event: Stories on the Porch.</span></strong></h2><p style="text-align: left;">Rock Ford Plantation | 881 Rockford Road | Lancaster, PA 17602 | 717-392-7223<br><a href="https://whereandwhen.com/events/49500/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638696035319999941">detail</gwmw></a></p><h2 style="text-align: left;"><span style="font-size: 18px;">Jun 22-30.&nbsp;</span><strong><span style="font-size: 18px;">Day Out With Thomas.</span></strong></h2><p style="text-align: left;">Strasburg Rail Road | 301 Gap Road | Ronks, PA 17572 | 717-687-7522, 866-725-9666<br><a href="https://whereandwhen.com/events/49492/"><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15584638696098779512558">detail</gwmw></a></p><p style="text-align: left;"><br></p>
    [images] => 
    [author] => W&W
    [publish] => Y
    [sidebardisplay] => N
    [notes] => 
    [region] => North,South,East,West
    [cats] => kids-stuff-kids-stuff,outdoor-recreation-outdoor-recreation
    [editdate] => 5/21/19 14:38
)

[28-Jun-2019 19:55:53 UTC] artkey[28]:

请求索引30的Var_dump:

[01-Jul-2019 13:06:51 UTC] Return Data Array[30]: Array
(
    [artkey] => 9c0e34da-e3ff-4d51-ae71-6b7003f40885
    [arttitle] => Pennsylvania Bucket List Challenge
    [issue] => Summer
    [year] => 2019
    [teaser] => <p>Take our Summer Bucket List Challenge</p>
    [story] => <p><img class="fr-dib fr-draggable" src="/_img/articles/PA SUMMER Bucket List Challenge 2019.jpg" style="width: 100%;"></p>
    [images] => 
    [author] => Wendy
    [publish] => Y
    [sidebardisplay] => N
    [notes] => 
    [region] => North,South,East,West
    [cats] => 
    [editdate] => 0000-00-00 00:00:00
)

[01-Jul-2019 13:06:51 UTC] artkey[30]:
[01-Jul-2019 13:06:51 UTC] Var Dump 30:

正如您在上面看到的,在索引为30的数组上的var dump中,我没有得到任何返回,但是print_r显示了数据,该功能还成功将所有数据返回给了jquery,我已经创建了可选的行和列,除了故事数据还不完整,不能只用引号引起来,这就是为什么我要使用此功能,如果我能得到所有数据就尝试保存数据。这对故事很有用,但是由于某些原因无法访问此艺术钥匙,不确定发生了什么,我将重新开始...

Front end return

0 个答案:

没有答案