470,848 Members | 1,709 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Checking information in an Array.

I have a function which reads a csv file into an array from there I want to
check if a value is in it.

Basically the csv files look like this:

23123233123,name,so**@one.com
23423412213,name2,so***@one.com
211111,name3,so***@one.com

My function looks like this:
function read_csv($filename, $delim=',')
{
$row = 0;
$dump = array();
$f = fopen ($filename,"r");
$size = filesize($filename)+1;
while ($data = fgetcsv($f, $size, $delim)) {
$dump[$row] = $data;
$row++;
}
fclose ($f);
return $dump;
}

$csv_array = read_csv(csvfile.csv);

I would like to now seach the array to see if a value is in there and do
something from there with it.

if (in_array(211111) == "true"){
echo "Your already in the file";
//note don't write to csv file
}

When I try to check to see if the value is in there It never tells me true
so I can run the commands that I want from there. If anyone can tell me
what I'm doing wrong please let me know.
Jul 17 '05 #1
2 1572
Tek9_AK a écrit le 02/05/2004 :
$csv_array = read_csv(csvfile.csv);

if (in_array(211111) == "true"){
echo "Your already in the file";
//note don't write to csv file
}

When I try to check to see if the value is in there It never tells me true
so I can run the commands that I want from there. If anyone can tell me
what I'm doing wrong please let me know.


Maybe boolean value TRUE is not the string "true"? (not sure about this
one)
And you missed the second argument of the in_array function... which is
the array you're searching in...
Try if(in_array(211111, $csv_array) == TRUE) {
Jul 17 '05 #2
Tek9_AK wrote:
I have a function which reads a csv file into an array from there I want to
check if a value is in it.

Basically the csv files look like this:

23123233123,name,so**@one.com
23423412213,name2,so***@one.com
211111,name3,so***@one.com

My function looks like this:
function read_csv($filename, $delim=',')
{
$row = 0;
$dump = array();
$f = fopen ($filename,"r");
$size = filesize($filename)+1;
while ($data = fgetcsv($f, $size, $delim)) {
$dump[$row] = $data;
$row++;
}
fclose ($f);
return $dump;
}

$csv_array = read_csv(csvfile.csv);

<?php
print_r($csv_array);
?>

And the resulting info is:
Array
(
[0] => Array
(
[0] => 23123233123
[1] => name
[2] => so**@one.com
)

[1] => Array
(
[0] => 23423412213
[1] => name2
[2] => so***@one.com
)

[2] => Array
(
[0] => 211111
[1] => name3
[2] => so***@one.com
)

[3] => Array
(
[0] =>
)

)

I would like to now seach the array to see if a value is in there and do
something from there with it.

if (in_array(211111) == "true"){
echo "Your already in the file";
//note don't write to csv file
}

if (in_array(211111, $csv_array)) {
echo "Your already in the file";
}

But this will not work!
$csv_array is composed of arrays. You can call in_array() for each of
the arrays in $csv_array:

<?php
$in = false;
foreach ($csv_array as $sub_array) {
if (in_array(211111, $sub_array)) {
$in = true;
}
}
if ($in) {
echo "Your already in the file";
//note don't write to csv file
}
?>

When I try to check to see if the value is in there It never tells me true
so I can run the commands that I want from there. If anyone can tell me
what I'm doing wrong please let me know.


Happy Coding :)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David P. Jessup | last post: by
3 posts views Thread by Wendy S | last post: by
26 posts views Thread by Sterten | last post: by
6 posts views Thread by Flip | last post: by
21 posts views Thread by jacob navia | last post: by
8 posts views Thread by The Cool Giraffe | last post: by
125 posts views Thread by jacob navia | last post: by
11 posts views Thread by Bryan Crouse | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.