Connecting Tech Pros Worldwide Help | Site Map

A little help with isset, in_array

Vernon Wenberg III
Guest
 
Posts: n/a
#1: Apr 10 '08
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?
Jerry Stuckle
Guest
 
Posts: n/a
#2: Apr 10 '08

re: A little help with isset, in_array


Vernon Wenberg III wrote:
Quote:
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.
jstucklex@attglobal.net
==================

Vernon Wenberg III
Guest
 
Posts: n/a
#3: Apr 10 '08

re: A little help with isset, in_array


Jerry Stuckle wrote:
Quote:
>
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.

}


Vernon Wenberg III
Guest
 
Posts: n/a
#4: Apr 10 '08

re: A little help with isset, in_array


Vernon Wenberg III wrote:
Quote:
Jerry Stuckle wrote:
Quote:
>>
>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.
Jerry Stuckle
Guest
 
Posts: n/a
#5: Apr 10 '08

re: A little help with isset, in_array


Vernon Wenberg III wrote:
Quote:
Vernon Wenberg III wrote:
Quote:
>Jerry Stuckle wrote:
Quote:
>>>
>>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.
jstucklex@attglobal.net
==================

Vernon Wenberg III
Guest
 
Posts: n/a
#6: Apr 11 '08

re: A little help with isset, in_array


Jerry Stuckle wrote:
Quote:
Vernon Wenberg III wrote:
Quote:
>Vernon Wenberg III wrote:
Quote:
>>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.
George Maicovschi
Guest
 
Posts: n/a
#7: Apr 11 '08

re: A little help with isset, in_array


On Apr 11, 3:45 am, Vernon Wenberg III <vwenb...@gmail.comwrote:
Quote:
Jerry Stuckle wrote:
Quote:
Vernon Wenberg III wrote:
Quote:
Vernon Wenberg III wrote:
>Jerry Stuckle wrote:
>
Quote:
Quote:
>>What's your actual code? In both of these examples you're just
>>throwing away the results of the function calls.
>
Quote:
Quote:
>This is the basic code ...
>
Quote:
Quote:
> // build array from db results
> while ($id_array = mysql_fetch_array($sql_id_matching_result)) {
> $real_item_id_array[] = $id_array['item_id'];
> }
>
Quote:
Quote:
> $item_link_md5 = 'test_var';
>
Quote:
Quote:
> if (isset($real_id_array[$item_link_md5])) {
> // do stuff here.
> }
>
Quote:
Quote:
EDIT: *real_item_id_array in the if statement.
>
Quote:
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:
>
Quote:
$real_id_array[0]
$real_id_array[1]
$real_id_array[2]
$real_id_array[3]
etc.
>
Quote:
So isset() will never return true.
>
Quote:
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.
Closed Thread


Similar PHP bytes