【问题标题】:CSV to PHP Associative Array and Filter DataCSV 到 PHP 关联数组和过滤数据
【发布时间】:2017-04-29 18:20:29
【问题描述】:

我正在使用 PHP 5.3+ 并尝试打开 CSV 文件,将信息存储为关联数组,然后能够过滤数组。

到目前为止,我已经设法使用(随时改进)打开 CSV 文件:

$all_rows = array();

$header = null;
if (($handle = fopen("files/my_data.csv", "r")) !== FALSE) {
  while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($header === null) {
       $header = $row;
       continue;
    }
  $all_rows[] = array_combine($header, $row);
  }

fclose($handle);
}

print_r($all_rows);

可以打印数据,但我正在努力弄清楚如何执行以下操作...

对于每一行,其中第 1 列大于或等于 1.0 且小于 3.9 计数,然后输出总计。在以下示例中,3 行中有 2 行数据符合条件。

仅示例数据:

/*
$row_1 = [
    ['col_1' => '1.1', 'col_2' => 'Product Name'],
];

$row_2 = [
    ['col_1' => '1.2', 'col_2' => 'Product Name'],
];

$row_3 = [
    ['col_1' => '4.0', 'col_2' => 'Product Name'],
];

*/

$row_1, 2, 3 和 $col_1, 2 ,3 等将是关联数组中列出的名称。

任何帮助将不胜感激,如果您需要任何进一步的信息,请询问

谢谢

詹姆斯

==============================

更新:这是 foreach 语句

$counter_total = 0;


// Count Sidebar Products Positions
foreach ($all_rows as $key => $value) {

 // I was missing the $key value in $all_rows[{here}]['ProductPosition']
 #echo $key; // ID Number of Row

 $counter_total++;

  if (($all_rows[$key]['ProductPosition'] >= 1) && ($all_rows[$key]['ProductPosition'] <= 9.9)){
    $sidebar_one++;   
   }
  elseif (($all_rows[$key]['ProductPosition'] >= 10) && ($all_rows[$key]['ProductPosition'] <= 19.9)){
    $sidebar_two++;  
  }
  elseif (($all_rows[$key]['ProductPosition'] >= 20) && ($all_rows[$key]['ProductPosition'] <= 29.9)){
    $sidebar_three++;  
  }

}


// Display Output - Testing Only 
echo '==================<br>';
echo 'Counter Total: '. $counter_total . '<br>';
echo 'Sidebar 1: '. $sidebar_one . '<br>';
echo 'Sidebar Page 2: '. $sidebar_page_two . '<br>';
echo 'Sidebar Page 3: '. $sidebar_page_three . '<br>';



// Show Sidebar 1 Products
foreach ($all_rows as $key => $value) {

 if (($all_rows[$key]['ProductPosition'] >= 1) && ($all_rows[$key]['ProductPosition'] <= 9.9)){
    #$sidebar_one++;     

    echo $key . ': ' . $all_rows[$key]['Queries']."<br />"; 
  }

}

但是,Show Sidebar 1 Products 显示所有产品并忽略计数器,即使测试显示中的计数器是正确的?莫名其妙!!

【问题讨论】:

  • 使用foreach() 循环遍历$all_rows 的内容。在循环中,测试您想要的列并将结果累积到一个变量中。
  • SO 不是免费的编码服务。您必须自己尝试解决问题。如果您无法使其正常工作,请发布您尝试过的内容,我们会帮助您解决问题。
  • 嗨 Barmer,我已经尝试过但没有任何效果,而不是发布各种尝试,我认为最好就我想要实现的目标寻求帮助。如果那不适合 SO,那么我很抱歉。
  • 如果您尝试过,那么您应该可以发布您的代码。然后我们会告诉你哪里出错了,你会从错误中吸取教训。
  • 我很同情并发布了代码。我想知道你的哪个部分有问题。循环? if 声明?给变量加一个数字得到总数?

标签: php arrays csv filter associative-array


【解决方案1】:

只需使用一个简单的foreach 循环来测试列并将其添加到总数中。

<?php

$all_rows = [
    ['ProductPosition' => '1.1', 'Queries' => 'Product 1'],
    ['ProductPosition' => '1.2', 'Queries' => 'Product 2'],
    ['ProductPosition' => '4.0', 'Queries' => 'Product 3'],
];

$counter_total = 0;
$sidebar_one = $sidebar_two = $sidebar_three = 0;

// Count Sidebar Products Positions
foreach ($all_rows as $value) {

 $counter_total++;

  if ($value['ProductPosition'] >= 1 && $value['ProductPosition'] <= 3.9){
    $sidebar_one++;   
   }
  elseif ($value['ProductPosition'] <= 19.9){
    $sidebar_two++;  
  }
  elseif ($value['ProductPosition'] <= 29.9){
    $sidebar_three++;  
  }

}


// Display Output - Testing Only 
echo '==================<br>' . "\n";
echo 'Counter Total: '. $counter_total . '<br>'. "\n";
echo 'Sidebar 1: '. $sidebar_one . '<br>'. "\n";
echo 'Sidebar Page 2: '. $sidebar_two . '<br>'. "\n";
echo 'Sidebar Page 3: '. $sidebar_three . '<br>'. "\n";

foreach ($all_rows as $key => $value) {

 if ($value['ProductPosition'] >= 1 && $value['ProductPosition'] <= 3.9){
    echo $key . ': ' . $value['Queries']."<br />\n"; 
  }

}

DEMO

【讨论】:

  • 感谢 Barmar,我发现我在 foreach 语句的 IF 部分缺少 $key ($row) 值,但我不确定它是否 100% 工作,因为第二部分不是正常运行。那是什么协议?我应该更新这篇文章吗?
  • 嗨 Barmar,我用额外的 foreach 语句更新了帖子。尽管第二个语句与第一个语句相同,但它似乎回显了 CSV 中的每个产品名称(“查询”),而不是之前正确计算的值之间的那些。如果这有意义吗?感谢您的帮助。
  • 感谢 Barmar,这个错字已经解决了问题,CSV 的其他值是 9.9,而不是应该是限制的 4.9。非常感谢您的帮助...这是漫长的一天!
猜你喜欢
  • 1970-01-01
  • 2012-12-22
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
相关资源
最近更新 更多