0

I'm using PHP 5.3+ and trying to open a CSV file, store the information as an associative array and then be able to filter the array.

So far i have managed to open the CSV file using (feel free to improve):

$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);

Which prints the data ok, but I am struggling to work out how to do something like the following...

For each Row, where Column 1 is greater than or equal to 1.0 and less than 3.9 Count then Output the Total. In the following example data 2 out of 3 rows would meet the criteria.

Example Data Only:

/*
$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 and $col_1, 2 ,3 etc would be the names listed in the associative array.

Any help would be appreciated, if you need any further information please ask

Thanks

James

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

UPDATE: Here is the foreach statements

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

}

However, the Show Sidebar 1 Products shows all products and ignores the counter, even though the counter in the test display was correct? Baffled!!

7
  • Use a foreach() loop to iterate over the contents of $all_rows. In the loop, test the columns you want and accumulate the result in a variable. Commented Apr 29, 2017 at 18:59
  • 1
    Hi Barmer, i have tried and nothing worked, rather than post the various attempts i thought it best to ask for help with a clean question on what i was trying to achieve. If that's not that suitable for SO then my apologies. Commented Apr 29, 2017 at 19:56
  • If you tried, then you should be able to post your code. Then we'll tell you where you went wrong, and you'll learn from your mistakes. Commented Apr 29, 2017 at 20:42
  • I've taken pity and posted the code. I wonder which part of it you had trouble with. the loop? the if statement? Adding a number to a variable to get a total? Commented Apr 29, 2017 at 20:46
  • When you use foreach() you don't need to write $all_rows[$key], you can use $value instead. That's the point of using foreach. Commented Apr 29, 2017 at 21:38

1 Answer 1

0

Just use a simple foreach loop that tests the column and adds it to a total.

<?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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Barmar, I found that i was missing the $key ($row) value in the IF part of my foreach statement, but i'm not sure its working 100% as the second part is not functioning correctly. Whats the protocol with that? Should i update this post?
Hi Barmar, i updated the post with the additional foreach statements. Even though the second statement is the same as the first, it seems to echo every product name ("queries") in the CSV and not the ones between the values it had previously calculated correctly. If that makes sense? Thank you for your help.
Thanks Barmar, that typo has fixed the problem, the CSV had other values to 9.9 and not the 4.9 that was supposed to be the limit. Much appreciate your help... It's been a long day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.