Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count multi-dimensional array 1

Status
Not open for further replies.

gmail2

Programmer
Jun 15, 2005
987
IE
I'm writing a function to upload a csv file and put each record into an array. However I want to count how many records I have when I put the data into the array. Count only returns how many fileds are in the array - I want to know how many records are in the array. How can I do this?
 
count returns the number of "rows" (using your nomenclature).

also consider using fgetcsv() (check out - there is a pre-built function already contributed by users).

the code below gives the answer "2" and not "3"
Code:
<?
$array = array 
		("record1"=>array
					(	"field1"=>"field1", 
						"field2"=>"field2",
						"field3"=>"field3"), 
		"record2"=>array(
						"field1"=>"a", 
						"field2"=>"b",
						"field3"=>"c"));
echo count($array);
?>
 
Yea sorry - I should have said I am using fgetcsv. I decided to just incremet a counter each time it loops through the array - however I'm still having problems. I'm a little new to PHP. Basically what I need to know is, when you declare an array in a while loop - is the array only valid while the loop is running? Here's what I've got:
Code:
$fileContent = Array();
$recordCount = 0;
$fileHandle = fopen($HTTP_POST_FILES['file']['tmp_name'],"r");
while($fileContent = fgetcsv($fileHandle,1000000,","))
        {

                $recordCount++;
                //print_r($fileContent);

        }
print_r($fileContent);
I've tried commenting out the $fileContent = Array() line also but regardless print_r doesn't return anything outside of the loop - it does inside the loop though. Can you help me?
 
right... a few things

* consider using $_FILES instead of the deprecated form.

* the array is valid within the scope of the script and not just within the while loop. the problem you are having is that you are redeclaring the $fileContent variable each iteration of the while loop so you will only have one record at the end (the last).

you either need to assign each row to an element of the array or to push fileContent into another array element inside each loop. the former is easier/cleaner!

try the code below

Code:
$fileHandle = fopen($_FILES['file']['tmp_name'],"rb"); //NOTE you should really do some testing for a validly uploaded file first.  see php.net for more details

while ($fileContent[] = fgetcsv($fileHandle)): //note that this function defaults to a comma delimeter and omitting the line length means it will always read all the line

//do nothing in the while loop

endwhile;
echo "the number of records is ". count($fileContent);
echo "<br/><pre>";
print_r($fileContent);
echo "</pre>";
 
Thanks for that jpadie. It looks like you were right, it was overwriting the array every time. And for some reason it's reading in a blank line at the end (not too sure why, have to look into that further). So that's why print_r wasn't giving me what I expected. Although I still don't quiet understand why you while loop reads the entire contents into the array and mine didn't. Is it just because I left off the []?

I tried omitting the line length before but I just got an error. I tried again now and I get the same thing - it says Wrong parameter count for function fgetcsv - what version of PHP are you running? I have no control over the version on the server - I think it's somewhere around 4.3 though.

Regarding checking for the validity of the file - I'm already doing that both client and server side but thanks for the suggestion all the same. Also, one final question. I done a check to see if the file error code was not equal to zero (ie did the file upload correctly). Then I check to see if the file is CSV format. However, if I just type in "anything.csv" in the input field it doesn't return an error code - it just executes the code that says file is not CSV format. Why doesn't it hit the error code seing that the file doesn't exist?

Anyway, thanks again. You've been a great help - much appreciated
 
happy to help

the fgetcsv function changed to make line length optional in 5.0.0 so that will be the cause, i guess!

re your question on file checking - i'm not sure how you can implement file upload validation client-side. i'll post the kind of code that i was talking about in this thread in an hour or so.
 
the fgetcsv function changed to make line length optional in 5.0.0 so that will be the cause, i guess!
I thought as much !!

re your question on file checking - i'm not sure how you can implement file upload validation client-side. i'll post the kind of code that i was talking about in this thread in an hour or so.

Sorry - I think you misunderstood the question !! Basically I've got a client side script that checks the last 3 characters of the input - if they're not CSV then the form never gets submitted. However, my question is that if the user just types in a name of a file that doesn't exist - then PHP still returns error code 0 (ie no error) - despite the fact that no file got uploaded !! It's not that important really because when I check the mime type it gets caught there anyway - but I just thought it seemed a bit strange.

Anyway, thanks again for all your help.
 
cross-purposes. i was meaning that one should always test for a validly uploaded file in the receiving script. this code is taken from the php.net site

Code:
$uploaddir = '/var/[URL unfurl="true"]www/uploads/';[/URL] //your upload directory
$uploadfile = $uploaddir . basename($_FILES['file']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)):
   echo "File is valid, and was successfully uploaded.\n";
else :
   echo "Possible file upload attack!\n";
endif;
echo "</pre>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top