Re: array problems
| From: | Eugene Lee | Date: | Tue, 25 Nov 2003 21:49:21 +0000 |
| Subject: | Re: array problems | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-170947@lists.php.net to get a copy of this message | ||
On Tue, Nov 25, 2003 at 03:45:11PM -0500, Curtis Maurand wrote:
:
: Hello,
: consider the following code (content.txt is tab delimited).
:
: $city = "Ipswitch";
: $content = fopen("content.txt", "r");
: $city_found = 0;
: while (!feof($content) && $city_found == 0)
: {
: $my_line = fgets($content, "r");
: $content_array = explode("\t",$my_line);
: if ($content_array == $city)
: {
: print("Matched on $content_array[0]");
: $city_found = 1;
: }
: }
: print("$content_array[0]<br>\n");
:
: Here's the trouble.
:
: inside the while loop $content_array is available to me.
: outside the loop $content_array is not available. What
: am I doing wrong?
Your if statement is wrong. You can't compare an array with a string.
And your $content_array is not guaranteed to exist when it reaches your
print() statement. You can test for existence with is_array(). But you
already have this knowledge via your $city_found variable. Use it.
Here's a revision (untested):
$city = "Ipswitch";
$content = fopen("content.txt", "r");
$city_found = false;
while (!feof($content))
{
$my_line = fgets($content, "r");
$content_array = explode("\t",$my_line);
if ($content_array[0] == $city)
{
print("Matched on $content_array[0]");
$city_found = true;
break;
}
}
if ($city_found)
{
print("$content_array[0]<br>\n");
}