#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    1
    Rep Power
    0
    I need to retrieve data from a text file.
    The text file contains data like this:

    a,b,c
    e,f,g
    h,i,j

    Every record is in a new line and has 3 fields.

    I already retrieved the data and slit it on the basis of n and stored it in an array($newarray) so that $newarray[0]=a,b,c.

    Now I need to further split the array so that it will give me a,b,c,separately and can be stored in diff variables.

    This entire process needs to be done for all records in the text file(which are separated by line breaks).

    I have already tried the following code for multidimensional arrays but does not work.
    $contents contains the entire content of the text file.

    $newArray=split("n",$contents);
    while (list($key,$value) = each($newArray)){
    echo "<br>the contents of the new Array are:$value";
    }

    $numlines=count($newArray);
    echo "<br>Count is:$numlines<br>";
    $n=0;
    $elementArray=array();
    for ($i=0;$i<$numlines;$i++)
    {
    $elementArray[$n]=split(",",$newArray[$i]);
    $n++;
    echo"First Element:$elementArray[$i][$n]<br>";
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Washington, USA
    Posts
    52
    Rep Power
    15
    <<I need to retrieve data from a text file.
    The text file contains data like this:
    a,b,c
    e,f,g
    h,i,j
    >>

    You can split by the comma since that is the delimiter.

    $array = split(",", $contents);
    $array[2] = eregi_replace("n", "", $array[2]);

    Line 2 will effective chomp the new line. You can also make your own chomp function like the one that exists in Perl.

    Function phpChomp($in) {
    $in = eregi_replace("n", "", $in);
    return($in);
    }

    Just call the function by passing an arg like this whenever you want to chomp a new line.

    $array[$index] = phpChomp($array[$index]);

    :smile:


    ------------------
    <UL TYPE=SQUARE>
    <LI> TD Scripts
    <LI> Script School
    <LI>
    php-scripts
    </UL>

IMN logo majestic logo threadwatch logo seochat tools logo