Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Verify an item in an array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Verify an item in an array

Postby I_like_php » Mon Jan 27, 2003 12:02 pm

How do I verify an item in an array?

For example if I have an array with the following things: apple, orange, pear.
and an apple is requested. How do I check that the item apple is in the array, so that I can return TRUE and if it is not in the array I can return false?
I_like_php
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jan 27, 2003 11:57 am

Postby Joan Garnet » Tue Jan 28, 2003 4:11 pm

Code: Select all
$os = array ("Mac", "NT", "Irix", "Linux");
if (in_array ("Irix", $os)) {
    print "Got Irix";
}

http://www.phpfreaks.com/phpmanual/page ... array.html
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Doesn't work

Postby I_like_php » Wed Jan 29, 2003 2:18 pm

This method doesn't seem to work.
Here is what I do.
I tested this way. I call $Row[Names] from the MySQL database. if I use the print() function it prints the following:
Jim
John
Jack
Then I do this:
$name = "John";
if (in_array ($name, $Row[Names])) {
print "Hello, $name";
}

Doesn't do anything.
I_like_php
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jan 27, 2003 11:57 am

Postby Redcircle » Fri Jan 31, 2003 1:45 am

try

Code: Select all
$name = "John";
if (in_array ($name, $Row)) {
print "Hello, $name";
}


$Row['Names'] is not an array it's a string. $Row is the actual array of the result.
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron