سؤال

I have been going round in circles with this and I'm hoping someone can help.

I have a .csv file containing data, example:

Weight, 10, 20, 30, 40

product1, 1, 2, 3, 4

product2, 2, 4, 6, 8

product3, 3, 6, 9, 12

What I need to do, using PHP, is select the value from the matrix where the row is "product2" and the number in that row corresponds with the nearest rounded match to a Weight of 27. The result should therefore be 6.

I've tried adding the csv rows to an array using fgetcsv and looping through to get the line where the first array item = product2, but I can't cross reference it with the Weight values from line 1.

I appreciate this csv is effectively upside down but this is the easiest way to manage it.

I can post some code I've tried so if it helps but, to be honest, it's all a bit of a mess so I was hoping someone could point me in the right direction with some working code.

Any help is appreciated! Thanks.

هل كانت مفيدة؟

المحلول 2

You should define what the nearest rounded match is. For 24 it should look the value for 20? I think as I understand what for you use it for 24 it should also be 30 otherwise you should define if round should be defined for a half of weights (first column) or not.

There is sample code that works as I think it should, so for 24 it will also choose 30

PHP file:


<?php

$name = 'product2';
$weight = 24;

$lines = explode("\n", file_get_contents("test.csv"));

$found= '';

$foundCost = false;

foreach ($lines as $line) {
  if (strpos($line,$name.',') === 0) {
     $found = $line;
     break;
  }
}






if ($found != '') {
    $found = explode(',', $found);
    $weights = explode(',',$lines[0]);

    $index = false;

    for ($i=1; $i<count($weights); ++$i) {
       if ($weights[$i] >= $weight)  {
           $index = $i;
           break;
       }       
    }
    if ($index !== false) {
        $foundCost = $found[$index];    
    }

}

if ($foundCost !== false) {

    echo "found ".$foundCost;
}

CSV file (I assumed there are no space after , as in ordinary CS file):

Weight,10,20,30,40
product1,1,2,3,4
product2,2,4,6,8
product3,3,6,9,12

نصائح أخرى

<?php
    $first_line;
    $other_lines_separated;
    $file=file('CSV_File_location'); //Put file contents in an array with each item being a line
    foreach($file as $f){
        //Get each line then break that line an into a array
        $values=explode(',',$f);
        //If this is the first line we want it separate
        if($f== $file[0]){
            $first_line=$values;
        }else{
            $other_lines_separated[]=$values;
        }
     }


//We now have an array with the first line and an array holding the other arrays with values separated
 //Go through each of the other arrays and do what you need

 for($i=0;$i<count($other_lines_separated); $i++){
      $other_line= $other_lines_separated[$i];
      for($y=0;$y<count($other_line); $y++){
         // in here we'll have access to any value we need
         if($other_line[$y] == "product1"){
          //We're on the product1 line
         }
         if($other_line[$y] == "product2"){
          //We're on the product2 line
         }
         if($other_line[$y] == "product3"){
          //We're on the product3 line
         }
         echo "currently at value ".$other_line[$y]."</br>";
      }
 }




?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top