0

I've ran into a problem when parsing a CSV (with 2 coloumns) Array ( [0] => Array ( [0] => oldcustomer 1 => newcostumer 4245945 [2] => 6773197 4260367 [3] => 6773381 4889300 [4] => 6764472 4740434 [5] => 6764555 4710449 [6] => 6764560

I want it to be in separate arrays and the newcostumerID is mixed with "oldcustomer ID"

CSV File:

oldcustomer;newcostumer
4245945;6773197
4260367;6773381
4889300;6764472
4740434;6764555
4710449;6764560
4699714;6766531
4451642;6775682
4534699;6775683
4378586;6775684
4711005;6775685
4502714;6775686
4288738;6775687

CSV Parse (stolen from here)

function csvstring_to_array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") {
// @author: Klemen Nagode
$array = array();
$size = strlen($string);
$columnIndex = 0;
$rowIndex = 0;
$fieldValue="";
$isEnclosured = false;
for($i=0; $i<$size;$i++) {

    $char = $string{$i};
    $addChar = "";

    if($isEnclosured) {
        if($char==$enclosureChar) {

            if($i+1<$size && $string{$i+1}==$enclosureChar){
                // escaped char
                $addChar=$char;
                $i++; // dont check next char
            }else{
                $isEnclosured = false;
            }
        }else {
            $addChar=$char;
        }
    }else {
        if($char==$enclosureChar) {
            $isEnclosured = true;
        }else {

            if($char==$separatorChar) {

                $array[$rowIndex][$columnIndex] = $fieldValue;
                $fieldValue="";

                $columnIndex++;
            }elseif($char==$newlineChar) {
                echo $char;
                $array[$rowIndex][$columnIndex] = $fieldValue;
                $fieldValue="";
                $columnIndex=0;
                $rowIndex++;
            }else {
                $addChar=$char;
            }
        }
    }
    if($addChar!=""){
        $fieldValue.=$addChar;

    }
}

if($fieldValue) { // save last field
    $array[$rowIndex][$columnIndex] = $fieldValue;
}
return $array;

}

Community
  • 1
  • 1
moffepoffe
  • 372
  • 1
  • 8

2 Answers2

1

Look at the explode function. Take your string and run explode("\n",$string) to get an array of rows. Then iterate over each row and explode on the ; character.

$rows = explode("\n",$string);
$oldcustomer = array();
$newcustomer = array();
foreach ($rows as $row) {
  $columns = explode(";",$row);
  $oldcustomer[] = $columns[0];
  $newcustomer[] = $columns[1];
}
print_r($oldcustomer);
print_r($newcustomer);
Bob Baddeley
  • 2,264
  • 1
  • 16
  • 22
0

You can use the following code to solve your problem

$row = -1;
$oldcustomer=array();
$newcustomer=array();
if (($handle = fopen("yourfile.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        if($row==-1){//Skipping first line
            $row++;
            continue;
        }
        $oldcustomer[$row]=$data[0];
        $newcustomer[$row]=$data[1];
        $row++;
    }
    fclose($handle);
    print_r($oldcustomer);
    print_r($newcustomer);
}
koder
  • 454
  • 6
  • 18