Announcement

Collapse
No announcement yet.

Delete item from array?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete item from array?

    Hi,

    I've got a file that contains 7 digit numbers in this format:

    1111111
    2222222
    3333333
    3432993

    I want to determine if a specific number is there and if so delete it from the file. For example, $pincode = "2222222";

    Here's what I've got so far:

    // Read the entire file into the variable $file_contents

    $pinfilename = '/home/public_html/custom/php/data/pincode.txt';
    $fp = fopen( $pinfilename, 'r' );
    $file_contents = fread( $fp, filesize( $pinfilename ) );
    fclose( $fp );

    Now that it is an array, how do I determine if $pincode is present and how do I delete it?

    Any help would be appreciated,

    Tim
    Five Star Review Script - Add reviews to your website!
    Mixed Martial Arts - Houston MMA Training
    Women's Self-Defense - Courses and DVDs available

  • #2
    You're almost there

    PHP4 has a function in_array()

    i think the syntax is in_array($needle,$haystack);

    before php4 was out I wrote a simple function that did the same =>
    (it's named slightly differently, so as not to barf if PHP4 is running on the server)

    function isin_array($the_array,$the_value) {
    for ($i=0; $i < count($the_array); $i++) {
    #print "\narray: " . $the_array[$i];
    #print "\nvalue: " . $the_value;
    if ($the_array[$i] == $the_value) {
    return true;
    }
    }
    } # END in_array()

    I would just read in the file, line by line and do something like
    while (current_line_of_file)
    if (isin_array($stuff_coming_in,$myvalue)) {
    $yep_its_there = true;
    }
    }

    if ($yep_its_there) {
    etc.........
    }
    hth

    Bealers



    Comment


    • #3
      Thanks! I'll try that out.

      Tim
      Five Star Review Script - Add reviews to your website!
      Mixed Martial Arts - Houston MMA Training
      Women's Self-Defense - Courses and DVDs available

      Comment

      Related Topics

      Collapse

      Working...
      X