CSV到PHP关联数组和筛选数据

CSV到PHP关联数组和筛选数据,php,arrays,csv,filter,associative-array,Php,Arrays,Csv,Filter,Associative Array,我正在使用PHP5.3+并尝试打开一个CSV文件,将信息存储为关联数组,然后能够过滤数组 到目前为止,我已成功使用(请随意改进)打开CSV文件: 它可以打印数据,但我正在努力解决如何执行以下操作 对于每一行,如果列1大于或等于1.0且小于3.9,则输出总计。在下面的示例中,三行数据中有两行符合标准 仅示例数据: /* $row_1 = [ ['col_1' => '1.1', 'col_2' => 'Product Name'], ]; $row_2 = [ ['c

我正在使用PHP5.3+并尝试打开一个CSV文件,将信息存储为关联数组,然后能够过滤数组

到目前为止,我已成功使用(请随意改进)打开CSV文件:

它可以打印数据,但我正在努力解决如何执行以下操作

对于每一行,如果列1大于或等于1.0且小于3.9,则输出总计。在下面的示例中,三行数据中有两行符合标准

仅示例数据:

/*
$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 />"; 
  }

}
$counter\u总计=0;
//统计侧边栏产品的位置
foreach($key=>$value的所有行){
//我在$all_行[{here}]['ProductPosition'中缺少$key值
#echo$key;//行的ID号
$counter_total++;
如果($all_行[$key]['ProductPosition']>=1)和($all_行[$key]['ProductPosition']=10)和($all_行[$key]['ProductPosition']=20)和($all_行[$key]['ProductPosition']]$value){

如果($all_rows[$key]['ProductPosition']>=1)和($all_rows[$key]['ProductPosition']],只需使用一个简单的
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"; 
  }

}

使用
foreach()
循环以迭代
$all_rows
的内容。在循环中,测试所需的列并将结果累积到变量中。因此,这不是免费的编码服务。您必须自己尝试解决问题。如果您无法使其工作,请发布您尝试的内容,我们将帮助您解决。嗨,Barmer,我尝试过,没有ing成功了,而不是发布各种尝试,我认为最好是就我试图实现的目标提出一个清晰的问题寻求帮助。如果这不适合,那么我道歉。如果你尝试了,那么你应该能够发布你的代码。然后我们会告诉你哪里出了错,你会从错误中吸取教训。我感到同情和遗憾测试了代码。我想知道您对代码的哪一部分有问题。循环?if
语句?向变量添加一个数字以获得总数?感谢Barmar,我发现我缺少$key($row)我的foreach语句的IF部分中的值,但我不确定它是否100%正常工作,因为第二部分没有正常工作。协议是什么?我应该更新此帖子吗?嗨,Barmar,我用其他foreach语句更新了帖子。尽管第二个语句与第一个语句相同,但它似乎反映了每个产品e(“查询”)在CSV中,而不是它以前正确计算的值之间的值。如果这有意义的话?感谢您的帮助。感谢Barmar,这个错误已经解决了问题,CSV有其他值到9.9,而不是应该是限制的4.9。非常感谢您的帮助…这是漫长的一天!
<?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"; 
  }

}