PDA

View Full Version : Delete item from array?


Tim Mousel
Tue 11th Jul '00, 8:23am
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

Bealers
Tue 11th Jul '00, 7:32pm
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

Tim Mousel
Sat 15th Jul '00, 6:23am
Thanks! I'll try that out.

Tim