Normalize csv to insert into database

时间:2016-05-11 11:29:43

标签: php csv

I have a csv that I converted into common array, and then, this array I will be insert into database :

So, this is the php code :

if (file_exists($csv)) {
      $file = fopen($csv, 'r'); // r flag is for readonly mode

      fgetcsv($file, 1000, ",");
         while (( $line = fgetcsv($file) ) !== false) { // if line exists
              $tryOne[] = $line; // add to array

         }
      fclose($file);
}

Then I try to normalize, like this :

$countTryOne = count($tryOne);
for($i=0; $i <= $countTryOne; $i++ ):
    $back=0;
    $no_container = 0;
    echo " | ";
    foreach($tryOne[$i] as $key => $value) :
        if($key == 6 && $value != ''){
             $no_container = $value;
             echo "Container = ". $no_container;
        }else{
             echo $key ." = ".  $value;
        }
       echo "  | ";
    endforeach;
         echo "<br>";
    endfor;

I get like this :

| 0 = 1 | 1 = 0.720*151.8 | 2 = DNA07X1A513041608A | 3 = 4120 | 4 = 4160 | 5 = 4896 | 6 = | 7 = |
| 0 = 2 | 1 = 0.720*151.8 | 2 = DNA07X1A513041607A | 3 = 4220 | 4 = 4260 | 5 = 5056 | 6 = | 7 = |
| 0 = 3 | 1 = 0.720*151.8 | 2 = DNA07X1A513041604A | 3 = 4250 | 4 = 4290 | 5 = 5088 | Container = PCIU2731046 | 7 = W0182029 |
| 0 = 4 | 1 = 0.720*151.8 | 2 = DNA07X1A513041601A | 3 = 4030 | 4 = 4070 | 5 = 4800 | 6 = | 7 = |
| 0 = 5 | 1 = 0.720*151.8 | 2 = DNA07X1C413041619A | 3 = 4270 | 4 = 4310 | 5 = 5104 | 6 = | 7 = |
| 0 = 6 | 1 = 0.720*151.8 | 2 = DNA07X1C413041618A | 3 = 4260 | 4 = 4300 | 5 = 5104 | 6 = | 7 = |
| 0 = | 1 = | 2 = | 3 = 25150 | 4 = 25390 | 5 = 30048 | 6 = | 7 = |
| 0 = 1 | 1 = 0.720*151.8 | 2 = DNA07X1C517041619A | 3 = 4340 | 4 = 4380 | 5 = 5216 | 6 = | 7 = |
| 0 = 2 | 1 = 0.720*151.8 | 2 = DNA07X1C413041609A | 3 = 4250 | 4 = 4290 | 5 = 5088 | 6 = | 7 = |
| 0 = 3 | 1 = 0.720*151.8 | 2 = DNA07X1A513041641A | 3 = 4390 | 4 = 4430 | 5 = 5280 | Container = PCIU1049200 | 7 = W0182030 |
| 0 = 4 | 1 = 0.720*151.8 | 2 = DNA07X1A513041640A | 3 = 4340 | 4 = 4380 | 5 = 5184 | 6 = | 7 = |
| 0 = 5 | 1 = 0.720*151.8 | 2 = DNA07X1A513041639A | 3 = 4330 | 4 = 4370 | 5 = 5152 | 6 = | 7 = |
| 0 = 6 | 1 = 0.720*151.8 | 2 = DNA07X1A513041619A | 3 = 3330 | 4 = 3370 | 5 = 3968 | 6 = | 7 = |
| 0 = | 1 = | 2 = | 3 = 24980 | 4 = 25220 | 5 = 29888 | 6 = | 7 = |
| 0 = 1 | 1 = 0.720*151.8 | 2 = DNA07X1A418041618A | 3 = 3965 | 4 = 4005 | 5 = 4704 | 6 = | 7 = |
| 0 = 2 | 1 = 0.720*151.8 | 2 = DNA07X1A513041621A | 3 = 4220 | 4 = 4260 | 5 = 5024 | 6 = | 7 = |
| 0 = 3 | 1 = 0.720*151.8 | 2 = DNA07X1B414041616A | 3 = 4840 | 4 = 4880 | 5 = 5800 | Container = PCIU2977472 | 7 = W0183312 |
| 0 = 4 | 1 = 0.720*151.8 | 2 = DNA07X1A418041633A | 3 = 4000 | 4 = 4040 | 5 = 4736 | 6 = | 7 = |
| 0 = 5 | 1 = 0.720*151.8 | 2 = DNA07X1A418041616A | 3 = 4060 | 4 = 4100 | 5 = 4832 | 6 = | 7 = |
| 0 = 6 | 1 = 0.720*151.8 | 2 = DNA07X1A418041615A | 3 = 4060 | 4 = 4100 | 5 = 4816 | 6 = | 7 = |
| 0 = | 1 = | 2 = | 3 = 25145 | 4 = 25385 | 5 = 29912 | 6 = | 7 = | 

Please see, in key= 6.Can I re loop again decrement so I can get into normal format like this :

| 0 = 1 | 1 = 0.720*151.8 | 2 = DNA07X1A513041608A | 3 = 4120 | 4 = 4160 | 5 = 4896 | 6 = PCIU2731046 | 7 = W0182029 |
| 0 = 2 | 1 = 0.720*151.8 | 2 = DNA07X1A513041607A | 3 = 4220 | 4 = 4260 | 5 = 5056 | 6 = PCIU2731046 | 7 = W0182029 |
| 0 = 3 | 1 = 0.720*151.8 | 2 = DNA07X1A513041604A | 3 = 4250 | 4 = 4290 | 5 = 5088 | 6 = PCIU2731046 | 7 = W0182029 |
| 0 = 4 | 1 = 0.720*151.8 | 2 = DNA07X1A513041601A | 3 = 4030 | 4 = 4070 | 5 = 4800 | 6 = PCIU2731046 | 7 = W0182029 |
| 0 = 5 | 1 = 0.720*151.8 | 2 = DNA07X1C413041619A | 3 = 4270 | 4 = 4310 | 5 = 5104 | 6 = PCIU2731046 | 7 = W0182029 |
| 0 = 6 | 1 = 0.720*151.8 | 2 = DNA07X1C413041618A | 3 = 4260 | 4 = 4300 | 5 = 5104 | 6 = PCIU2731046 | 7 = W0182029 |

| 0 = 1 | 1 = 0.720*151.8 | 2 = DNA07X1C517041619A | 3 = 4340 | 4 = 4380 | 5 = 5216 | 6 = PCIU1049200 | 7 = W0182030 |
| 0 = 2 | 1 = 0.720*151.8 | 2 = DNA07X1C413041609A | 3 = 4250 | 4 = 4290 | 5 = 5088 | 6 = PCIU1049200 | 7 = W0182030 |
| 0 = 3 | 1 = 0.720*151.8 | 2 = DNA07X1A513041641A | 3 = 4390 | 4 = 4430 | 5 = 5280 | 6 = PCIU1049200 | 7 = W0182030 |
| 0 = 4 | 1 = 0.720*151.8 | 2 = DNA07X1A513041640A | 3 = 4340 | 4 = 4380 | 5 = 5184 | 6 = PCIU1049200 | 7 = W0182030 |
| 0 = 5 | 1 = 0.720*151.8 | 2 = DNA07X1A513041639A | 3 = 4330 | 4 = 4370 | 5 = 5152 | 6 = PCIU1049200 | 7 = W0182030 |
| 0 = 6 | 1 = 0.720*151.8 | 2 = DNA07X1A513041619A | 3 = 3330 | 4 = 3370 | 5 = 3968 | 6 = PCIU1049200 | 7 = W0182030 |

| 0 = 1 | 1 = 0.720*151.8 | 2 = DNA07X1A418041618A | 3 = 3965 | 4 = 4005 | 5 = 4704 | 6 = PCIU2977472 | 7 = W0183312 |
| 0 = 2 | 1 = 0.720*151.8 | 2 = DNA07X1A513041621A | 3 = 4220 | 4 = 4260 | 5 = 5024 | 6 = PCIU2977472 | 7 = W0183312 |
| 0 = 3 | 1 = 0.720*151.8 | 2 = DNA07X1B414041616A | 3 = 4840 | 4 = 4880 | 5 = 5800 | 6 = PCIU2977472 | 7 = W0183312 |
| 0 = 4 | 1 = 0.720*151.8 | 2 = DNA07X1A418041633A | 3 = 4000 | 4 = 4040 | 5 = 4736 | 6 = PCIU2977472 | 7 = W0183312 |
| 0 = 5 | 1 = 0.720*151.8 | 2 = DNA07X1A418041616A | 3 = 4060 | 4 = 4100 | 5 = 4832 | 6 = PCIU2977472 | 7 = W0183312 |
| 0 = 6 | 1 = 0.720*151.8 | 2 = DNA07X1A418041615A | 3 = 4060 | 4 = 4100 | 5 = 4816 | 6 = PCIU2977472 | 7 = W0183312 |

1 个答案:

答案 0 :(得分:0)

您的条件if($key == 6 && $value != '')表示密钥不是6 或值为空执行其他分支echo $key ." = ". $value;

您可能希望包含额外的elseif条件。