473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1659
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David P. Jessup | last post by:
Good day folks. Within an ASP I'm working on I need to check whether an array is empty or not. Code: Dim somearray() 'other code: array might have been populated, maybe not if somearray()...
3
by: Wendy S | last post by:
I have this working, but I don't think it's done efficiently or the best way: <a href="javascript:setAllAccounts(true)">check all</a> <a href="javascript:setAllAccounts(false)">clear all</a> ...
26
by: Sterten | last post by:
when I define int R; and then later access it with x=R;C=7; .... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. Is there a way to prevent this ? Is...
6
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
16
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
8
by: The Cool Giraffe | last post by:
I have a dynamically declared array of integers and i'd like to check the number of elements. For some reason, the expected sizeof(mtx)/sizeof(mtx) doesn't work. I've been googling and i got a...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.