A little help with isset, in_array
A little help with isset, in_array
am 10.04.2008 03:39:23 von Vernon Wenberg III
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?
Re: A little help with isset, in_array
am 10.04.2008 05:23:19 von Jerry Stuckle
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.
jstucklex@attglobal.net
==================
Re: A little help with isset, in_array
am 10.04.2008 05:53:29 von Vernon Wenberg III
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.
}
Re: A little help with isset, in_array
am 10.04.2008 05:55:07 von Vernon Wenberg III
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.
Re: A little help with isset, in_array
am 10.04.2008 23:19:54 von Jerry Stuckle
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.
jstucklex@attglobal.net
==================
Re: A little help with isset, in_array
am 11.04.2008 02:45:22 von Vernon Wenberg III
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.
Re: A little help with isset, in_array
am 11.04.2008 09:17:20 von George Maicovschi
On Apr 11, 3:45 am, Vernon Wenberg III wrote:
> 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.