Re: [PHP] fgetcsv doesn't return an array?

2012-03-18 Thread Lester Caine

Tamara Temple wrote:

On Thu, 15 Mar 2012 12:09:53 -0500, Jay Blanchard
 sent:


I thought that fgetcsv returned an array. I can work with it like an
array but I get the following warning when using it

|Warning:  implode(): Invalid arguments passed on line 155

154$csvCurrentLine = fgetcsv($csvFile, 4096, ',');
155$currentLine = implode(",", $csvCurrentLine);
|

Everything that I have read online indicates that it is because
$csvCurrentLine is not declared as an array. Adding a line to the code

153 |$csvCurrentLine = array();

Does not get rid of the warning. The warning isn't interfering with the
script working, but it us annoying. Does anyone know the solution?



Just want to clarify something not directly related to the OP's question:

This:


153 |$csvCurrentLine = array();


Does not set a *type* for $csvCurrentLine, so if you're next line is this:


154$csvCurrentLine = fgetcsv($csvFile, 4096, ',');


Any previous assignments are lost; the notion that presetting $csvCurrentLine to
an array has no bearing when you then immediately set the same variable to
something else.

The "type" $csvCurrentLine has after line 154 is entirely dependent on what
fgetcsv() is returning, and nothing that happened prior to line 154.


In this case, Jay's attempt to fix the problem was simply wrong. fgetcsv may 
well not return an array, and the error in this code was simply that situation. 
If an array is not returned by fgetcsv then there was an error, hence line 155 
fails every time you hit the 'EOF' condition.


--
Lester Caine - G8HFL
-
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-18 Thread Tamara Temple
On Thu, 15 Mar 2012 12:09:53 -0500, Jay Blanchard  
 sent:



I thought that fgetcsv returned an array. I can work with it like an
array but I get the following warning when using it

|Warning:  implode(): Invalid arguments passed on line 155

154 $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
155 $currentLine = implode(",", $csvCurrentLine);
|

Everything that I have read online indicates that it is because
$csvCurrentLine is not declared as an array. Adding a line to the code

153 |$csvCurrentLine = array();

Does not get rid of the warning. The warning isn't interfering with the
script working, but it us annoying. Does anyone know the solution?

Thanks!

Jay
|


Just want to clarify something not directly related to the OP's question:

This:


153 |$csvCurrentLine = array();


Does not set a *type* for $csvCurrentLine, so if you're next line is this:


154 $csvCurrentLine = fgetcsv($csvFile, 4096, ',');


Any previous assignments are lost; the notion that presetting  
$csvCurrentLine to an array has no bearing when you then immediately  
set the same variable to something else.


The "type" $csvCurrentLine has after line 154 is entirely dependent on  
what fgetcsv() is returning, and nothing that happened prior to line  
154.



--
Tamara Temple
   aka tamouse__

May you never see a stranger's face in the mirror

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-16 Thread Jay Blanchard

[snip]
 Not sure why, but ok. Thanks everyone!

An empty line would lead to $csvCurrentLine == array() which as a boolean would 
be false but would not necessarily mean EOF. By using === to check the value 
the expression will only evaluate to false at EOF or an error.

-Stuart

[/snip]

Makes perfect sense! I just wasn't reading it right.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Stuart Dallas
On 15 Mar 2012, at 20:26, Jay Blanchard wrote:

> [snip]
>> Please, don't do that. Use this instead:
>> while (($csvCurrentLine = fgetcsv($csvFile, 4096, ',')) !== FALSE) {
>> }
>> [/snip]
> 
> Not sure why, but ok. Thanks everyone!

An empty line would lead to $csvCurrentLine == array() which as a boolean would 
be false but would not necessarily mean EOF. By using === to check the value 
the expression will only evaluate to false at EOF or an error.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Jay Blanchard
[snip]
> Please, don't do that. Use this instead:
> while (($csvCurrentLine = fgetcsv($csvFile, 4096, ',')) !== FALSE) {
> }
> [/snip]

Not sure why, but ok. Thanks everyone!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Matijn Woudt
On Thu, Mar 15, 2012 at 6:40 PM, Andrew Ballard  wrote:
> On Thu, Mar 15, 2012 at 1:29 PM, Jay Blanchard
>  wrote:
>> On 3/15/2012 12:23 PM, Matijn Woudt wrote:
>>>
>>> On Thu, Mar 15, 2012 at 6:09 PM, Jay Blanchard
>>>   wrote:

 I thought that fgetcsv returned an array. I can work with it like an
 array
 but I get the following warning when using it

 |Warning:  implode(): Invalid arguments passed on line 155

 154     $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
 155     $currentLine = implode(",", $csvCurrentLine);
 |

 Everything that I have read online indicates that it is because
 $csvCurrentLine is not declared as an array. Adding a line to the code

 153 |$csvCurrentLine = array();

 Does not get rid of the warning. The warning isn't interfering with the
 script working, but it us annoying. Does anyone know the solution?

 Thanks!

 Jay
 |
>>>
>>> Are you using this in a loop or something? fgetcsv returns FALSE on
>>> errors, including End Of File.
>>>
>>> [/snip]
>>
>> I am using it in a loop. End Of File is an error?
>
> Yes.  "fgetcsv() returns NULL if an invalid handle is supplied or
> FALSE on other errors, including end of file."
>
> I usually use it in a while loop like this:
>
> 
> while ($row = fgetcsv($csvFilePointer)) {
>    // ... process the row
> }
>
> ?>

Please, don't do that. Use this instead:
while (($csvCurrentLine = fgetcsv($csvFile, 4096, ',')) !== FALSE) {
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Lester Caine

Jay Blanchard wrote:

154 $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
155 $currentLine = implode(",", $csvCurrentLine);



I am using it in a loop. End Of File is an error?


You certainly need to break out on $csvCurrentLine = false which indicates end 
of file at least.


My own 'loop' is
$row=0;
while (($data = fgetcsv($handle, 800, ",")) !== FALSE) {
  if ( $row ) $contact->RecordLoad( $data, $row );
$row++;
}
The row bit strips the header line from the csv file ...

--
Lester Caine - G8HFL
-
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Andrew Ballard
On Thu, Mar 15, 2012 at 1:29 PM, Jay Blanchard
 wrote:
> On 3/15/2012 12:23 PM, Matijn Woudt wrote:
>>
>> On Thu, Mar 15, 2012 at 6:09 PM, Jay Blanchard
>>   wrote:
>>>
>>> I thought that fgetcsv returned an array. I can work with it like an
>>> array
>>> but I get the following warning when using it
>>>
>>> |Warning:  implode(): Invalid arguments passed on line 155
>>>
>>> 154     $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
>>> 155     $currentLine = implode(",", $csvCurrentLine);
>>> |
>>>
>>> Everything that I have read online indicates that it is because
>>> $csvCurrentLine is not declared as an array. Adding a line to the code
>>>
>>> 153 |$csvCurrentLine = array();
>>>
>>> Does not get rid of the warning. The warning isn't interfering with the
>>> script working, but it us annoying. Does anyone know the solution?
>>>
>>> Thanks!
>>>
>>> Jay
>>> |
>>
>> Are you using this in a loop or something? fgetcsv returns FALSE on
>> errors, including End Of File.
>>
>> [/snip]
>
> I am using it in a loop. End Of File is an error?

Yes.  "fgetcsv() returns NULL if an invalid handle is supplied or
FALSE on other errors, including end of file."

I usually use it in a while loop like this:



Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Jim Lucas

On 03/15/2012 10:09 AM, Jay Blanchard wrote:

I thought that fgetcsv returned an array. I can work with it like an
array but I get the following warning when using it

|Warning: implode(): Invalid arguments passed on line 155

154 $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
155 $currentLine = implode(",", $csvCurrentLine);
|

Everything that I have read online indicates that it is because
$csvCurrentLine is not declared as an array. Adding a line to the code

153 |$csvCurrentLine = array();

Does not get rid of the warning. The warning isn't interfering with the
script working, but it us annoying. Does anyone know the solution?

Thanks!

Jay
|



From the manual

"fgetcsv() returns NULL if an invalid handle is supplied or FALSE on 
other errors, including end of file."


do this

154 $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
var_dump($csvCurrentLine);
155 $currentLine = implode(",", $csvCurrentLine);

What does it say about the variable from the failing line?

--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Jay Blanchard

On 3/15/2012 12:23 PM, Matijn Woudt wrote:

On Thu, Mar 15, 2012 at 6:09 PM, Jay Blanchard
  wrote:

I thought that fgetcsv returned an array. I can work with it like an array
but I get the following warning when using it

|Warning:  implode(): Invalid arguments passed on line 155

154 $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
155 $currentLine = implode(",", $csvCurrentLine);
|

Everything that I have read online indicates that it is because
$csvCurrentLine is not declared as an array. Adding a line to the code

153 |$csvCurrentLine = array();

Does not get rid of the warning. The warning isn't interfering with the
script working, but it us annoying. Does anyone know the solution?

Thanks!

Jay
|

Are you using this in a loop or something? fgetcsv returns FALSE on
errors, including End Of File.

[/snip]

I am using it in a loop. End Of File is an error?


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] fgetcsv doesn't return an array?

2012-03-15 Thread Matijn Woudt
On Thu, Mar 15, 2012 at 6:09 PM, Jay Blanchard
 wrote:
> I thought that fgetcsv returned an array. I can work with it like an array
> but I get the following warning when using it
>
> |Warning:  implode(): Invalid arguments passed on line 155
>
> 154     $csvCurrentLine = fgetcsv($csvFile, 4096, ',');
> 155     $currentLine = implode(",", $csvCurrentLine);
> |
>
> Everything that I have read online indicates that it is because
> $csvCurrentLine is not declared as an array. Adding a line to the code
>
> 153 |$csvCurrentLine = array();
>
> Does not get rid of the warning. The warning isn't interfering with the
> script working, but it us annoying. Does anyone know the solution?
>
> Thanks!
>
> Jay
> |

Are you using this in a loop or something? fgetcsv returns FALSE on
errors, including End Of File.

- Matijn

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php