473,385 Members | 2,004 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,385 software developers and data experts.

A little help with isset, in_array

I am trying to search within an array to see if there is a match, but I
can't get my head around these functions which are supposed to return
TRUE or FALSE.

First I get a list of values from a database and build an array with it
with numerical indexes.

When I finally do a comparison using both ...

in_array($item_link_md5, $real_id_array);
isset($real_id_array[$item_link_md5]);

I get a number as it's return value and it seems to be the total number
of values in the array, not a simple TRUE or FALSE.

Where am I going wrong here?
Apr 10 '08 #1
6 1611
Vernon Wenberg III wrote:
I am trying to search within an array to see if there is a match, but I
can't get my head around these functions which are supposed to return
TRUE or FALSE.

First I get a list of values from a database and build an array with it
with numerical indexes.

When I finally do a comparison using both ...

in_array($item_link_md5, $real_id_array);
isset($real_id_array[$item_link_md5]);

I get a number as it's return value and it seems to be the total number
of values in the array, not a simple TRUE or FALSE.

Where am I going wrong here?
What's your actual code? In both of these examples you're just throwing
away the results of the function calls.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Apr 10 '08 #2
Jerry Stuckle wrote:
>
What's your actual code? In both of these examples you're just throwing
away the results of the function calls.
This is the basic code ...

// build array from db results
while ($id_array = mysql_fetch_array($sql_id_matching_result)) {
$real_item_id_array[] = $id_array['item_id'];
}

$item_link_md5 = 'test_var';

if (isset($real_id_array[$item_link_md5])) {

// do stuff here.

}
Apr 10 '08 #3
Vernon Wenberg III wrote:
Jerry Stuckle wrote:
>>
What's your actual code? In both of these examples you're just
throwing away the results of the function calls.

This is the basic code ...

// build array from db results
while ($id_array = mysql_fetch_array($sql_id_matching_result)) {
$real_item_id_array[] = $id_array['item_id'];
}

$item_link_md5 = 'test_var';

if (isset($real_id_array[$item_link_md5])) {

// do stuff here.

}

EDIT: *real_item_id_array in the if statement.
Apr 10 '08 #4
Vernon Wenberg III wrote:
Vernon Wenberg III wrote:
>Jerry Stuckle wrote:
>>>
What's your actual code? In both of these examples you're just
throwing away the results of the function calls.

This is the basic code ...

// build array from db results
while ($id_array = mysql_fetch_array($sql_id_matching_result)) {
$real_item_id_array[] = $id_array['item_id'];
}

$item_link_md5 = 'test_var';

if (isset($real_id_array[$item_link_md5])) {
// do stuff here.
}


EDIT: *real_item_id_array in the if statement.
OK, this is checking to see if $real_id_array['test_var'] is set.
However, you added them with default indexes, so what you have in the
array is:

$real_id_array[0]
$real_id_array[1]
$real_id_array[2]
$real_id_array[3]
etc.

So isset() will never return true.

What are you TRYING to do?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Apr 10 '08 #5
Jerry Stuckle wrote:
Vernon Wenberg III wrote:
>Vernon Wenberg III wrote:
>>Jerry Stuckle wrote:

What's your actual code? In both of these examples you're just
throwing away the results of the function calls.
This is the basic code ...

// build array from db results
while ($id_array = mysql_fetch_array($sql_id_matching_result)) {
$real_item_id_array[] = $id_array['item_id'];
}

$item_link_md5 = 'test_var';

if (isset($real_id_array[$item_link_md5])) {
// do stuff here.
}


EDIT: *real_item_id_array in the if statement.

OK, this is checking to see if $real_id_array['test_var'] is set.
However, you added them with default indexes, so what you have in the
array is:

$real_id_array[0]
$real_id_array[1]
$real_id_array[2]
$real_id_array[3]
etc.

So isset() will never return true.

What are you TRYING to do?
I'm trying to compare values from the DB which are now in an array to
the a new value to find out if the new value exists in the DB.

Thank you for the explanation. I've gotten it to work now after you
pointed out the creation of my array was where I went wrong. I thought
that isset was just giving me quirky results.

Much thanks.
Apr 11 '08 #6
On Apr 11, 3:45 am, Vernon Wenberg III <vwenb...@gmail.comwrote:
Jerry Stuckle wrote:
Vernon Wenberg III wrote:
Vernon Wenberg III wrote:
Jerry Stuckle wrote:
>>What's your actual code? In both of these examples you're just
throwing away the results of the function calls.
>This is the basic code ...
> // build array from db results
while ($id_array = mysql_fetch_array($sql_id_matching_result)) {
$real_item_id_array[] = $id_array['item_id'];
}
> $item_link_md5 = 'test_var';
> if (isset($real_id_array[$item_link_md5])) {
// do stuff here.
}
EDIT: *real_item_id_array in the if statement.
OK, this is checking to see if $real_id_array['test_var'] is set.
However, you added them with default indexes, so what you have in the
array is:
$real_id_array[0]
$real_id_array[1]
$real_id_array[2]
$real_id_array[3]
etc.
So isset() will never return true.
What are you TRYING to do?

I'm trying to compare values from the DB which are now in an array to
the a new value to find out if the new value exists in the DB.

Thank you for the explanation. I've gotten it to work now after you
pointed out the creation of my array was where I went wrong. I thought
that isset was just giving me quirky results.

Much thanks.
Then the if should be:
if (in_array($item_link_md5,$real_item_id_array))
//do stuff here

Cheers,
George.
Apr 11 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: AJ Z | last post by:
I am using in_array() to search for a value ("other"), in order to validate a form. If I pass $_POST as the array to search PHP says that it is an invalid datatype. It is an array and if I copy...
3
by: Tom Barnes | last post by:
Check out this code: // Start Code ------------- function test_in_array($val) { $a = array('key' => $val); printf("in_array: %d, value:%s<BR>", in_array('key', $a), $a); } test_in_array(0);...
2
by: ahevans | last post by:
HI, I have the following scenario if (isset($area)) { if ($area === 'green') { if (isset($task)) {
0
by: doffer | last post by:
I want to make a portfoliosystem where user can register and get their own portfolio... I've started the developer work, but I'm stuck on the image upload part... I'm experiencing some problems...
7
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
6
by: jeffsnox | last post by:
Hi, I have multiple checkboxes on the same form as follows: <input type='checkbox' name='cbtype' value='1'> <input type='checkbox' name='cbtype' value='2'> <input type='checkbox'...
5
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4. Let's say I have two scalars, $list1 and $list2. Both are comma separated lists of ordered, unsigned, unique integers. An example would be $list1 = "2,26,345";...
3
by: Sonnich | last post by:
The following code is an exact copy of my current code. The idea is to check 2 parts (of string arrays), and add those only once to a common array. I check for existance of an array in a string,...
2
by: Nosferatum | last post by:
This script is meant to limit access by sessions, using username and password from mysql db and redirect users after login according to a given value belonging to each user in the db (10,20,30,40)....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.