Think I'm running in circles here.
I have a CSV file which the fields are separated by ';'.
The last field contains e.g. 52451 Butikkhandel med elektriske husholdningsapparater, radio og fjernsyn, 51874 Engroshandel med maskiner og utstyr til handel, transport og tjenesteyting ellers

Now I need to extract the numbers and put them into an array.
So I get an array containing 52451 and 51874. There may be more or fewer numbers.

Here's my code so far:

	$fp = fopen ("DB/TestData/testdata2.csv","r");

echo "File opened<br>";
$arrOrganization = array ();
$arrAddress = array();
$arrPerson = array();
$arrBusinessArea = array();
$arrBA = array();

while ($arrData = fgetcsv ($fp, 1000, ";"))
{
	array_push($arrOrganization, $arrData[0], $arrData[1], $arrData[5], $arrData[6], $arrData[7]);
	array_push($arrAddress, $arrData[0], $arrData[2], $arrData[3]);
	array_push($arrPerson, $arrData[4]);
	array_push($arrBusinessArea, $arrData[8]);
	$arrBA = explode(",",$arrBusinessArea[0]); // <-- This is wrong
}

fclose ($fp);


It's this line I have problems with:
$arrBA = explode(";",$arrBusinessArea[0]);

Eventually it all goes into a database.

Any help apprechiated.

    honestly, i'm not exactly sure what's the problem.
    (i also don't understand what data you have in your csv file)

    either way, it would seem that the line
    $arrBA = explode(";",$arrBusinessArea[0]);

    would not work because you are already spliting up the data by the semicolon
    while ($arrData = fgetcsv ($fp, 1000, ";")) ...

    in the code you wrote though, you have a comma instead of a semicolon. that seems like it would work. but again, i dunno what's your ultimate goal. are you trying to get just the numbers?

    Is the problem that the array $arrBA is just getting the last value?

    out of curiosity, what kind of data does $arrBusinessArea[0] output?

    -- sijis

      I have a CSV file. Each lines contains data separated with ;
      The file can be as much as 15.000 lines.

      I have all in all 9 fields. From [0] to [8] on each line.
      The info will be inserted into 4 different tables.

      $arrOrganization <- here goes company info
      $arrAddress <- company address
      $arrPerson <- contact person
      $arrBusinessArea <- working fields for a company.

      I have no problem extracting data and putting them in the 3 first arrays / tables)

      The problem is the last field. Field [8]

      It may contain just one business area:
      51874 {Busines desc witch commas}

      Or many business areas:
      51874 bus desc 1, bus desc 2, 51674 bus desc 1, bus desc 2

      I need to go in and extract the numbers 51874 and 51674.

      Basically it all comes down to reading a line and extracting the 5 digit numbers and storing them in an array.

        ahh, i see what your trying to do.
        its kind of late for me, so my response can be a bit odd.
        my first impression is to do this.

        split the field [8] by commas, and then split that action into spaces. then use the is_int() function and compare it to the string.
        if the string is an int store it, otherwise, continue on.

        //pseudo code
        $arr1 = explode(',', $arr[8]);

        foreach($arr1 as $key){
        $temp = explode(' ', $arr[key]);

        foreach($temp as $key1){
        if(is_int($key1)){
        $save .= $key;
        }
        }
        }

        i'm not good with array, but something like that would work... i'm sure there's a better solution.

        hth

        sijis

          exactly what I was going to suggest sijis. That is the best way to do it.

            Thanks mate.

            It worked... almost.
            I had to use is_numeric() insteadof is_int().

            Thanks for the tip!

              Write a Reply...