Frustrated and confused

Frustrated and confused

am 31.10.2007 03:25:39 von dysfunct

I have some code in which there are a bunch of objects. The objects
are in arrays. When checking one of the arrays for a number thats in
a variable in an object, I cant seem to get the search to work.
However I can get the search to work by putting in one instance of
that variable. But then comparing the two shows they're both the same.
I don't get how if they're both the same one works and one doesn't.
This is from inside one of the foreach statements:

[code]

if (in_array($val->onumber,$ponum))
echo "FOUND VARIABLE";
if (in_array(358737,$ponum))
echo "FOUND THE LITERAL";
if ($val->onumber == 358737)
echo "WHYWHYWHY";

[/code]

The output is FOUND THE LITERALWHYWHYWHY. Why will it find the value
directly but not the variable, when it admits they're the same?

BTW, the in_array is just what I was using to check this. What I
really want to do is an array_search, that was doing the same thing.

Any suggestions?

Re: Frustrated and confused

am 31.10.2007 04:09:20 von Juliette

dysfunct wrote:
> I have some code in which there are a bunch of objects. The objects
> are in arrays. When checking one of the arrays for a number thats in
> a variable in an object, I cant seem to get the search to work.
> However I can get the search to work by putting in one instance of
> that variable. But then comparing the two shows they're both the same.
> I don't get how if they're both the same one works and one doesn't.
> This is from inside one of the foreach statements:
>
> [code]
>
> if (in_array($val->onumber,$ponum))
> echo "FOUND VARIABLE";
> if (in_array(358737,$ponum))
> echo "FOUND THE LITERAL";
> if ($val->onumber == 358737)
> echo "WHYWHYWHY";
>
> [/code]
>
> The output is FOUND THE LITERALWHYWHYWHY. Why will it find the value
> directly but not the variable, when it admits they're the same?
>
> BTW, the in_array is just what I was using to check this. What I
> really want to do is an array_search, that was doing the same thing.
>
> Any suggestions?
>


Can you try this and tell me what you get ?

if ($val->onumber === 358737)
echo "SEE, THEY ARE REALLY THE SAME";

Re: Frustrated and confused

am 31.10.2007 04:18:23 von dysfunct

Hmmm... got nothing. Whats going on here?

Re: Frustrated and confused

am 31.10.2007 07:52:05 von luiheidsgoeroe

On Wed, 31 Oct 2007 04:18:23 +0100, dysfunct wrote:

> Hmmm... got nothing. Whats going on here?

var_dump($val->onumber,$ponum); to the rescue....
--
Rik Wasmus

Re: Frustrated and confused

am 31.10.2007 13:56:19 von dysfunct

Yeah you guys are right, something is going on with the types. So I
made a search function:

function array_key_search($num, $array){
$found = array();
$n = 0;
foreach ($array as $k => $V){
settype($v,string);
echo gettype($v);
echo gettype($num);
if (strncasecmp($v,$num,6)==0){
$found[$n] = $k;
$n += 1;} }

return $found;
}

I added the echo gettype()'s in there just to see what it was
comparing and they both end up being strings in all cases. Still
doesn't wanna give me any results! I am befuddled.

Re: Frustrated and confused

am 31.10.2007 14:02:21 von dysfunct

I just did this:

function array_isearch($num, $array){
$found = array();
$n = 0;
foreach ($array as $k => $V){
settype($v,string);
echo "The ".gettype($v)." ".$v." does NOT equal ";
echo "the ".gettype($num)." ".$num."
";
if (strncasecmp($v,$num,6)==0){
$found[$n] = $k;
$n += 1;} }

return $found;
}

The thing is, I'm getting "The string does NOT equal the string
[6digit number]." It's not picking up that value from the array...
hmm......

Re: Frustrated and confused

am 31.10.2007 14:09:07 von luiheidsgoeroe

On Wed, 31 Oct 2007 14:02:21 +0100, dysfunct wro=
te:

> I just did this:
>
> function array_isearch($num, $array){
> $found =3D array();
> $n =3D 0;
> foreach ($array as $k =3D> $V){
> settype($v,string);
> echo "The ".gettype($v)." ".$v." does NOT equal ";
> echo "the ".gettype($num)." ".$num."
";
> if (strncasecmp($v,$num,6)==0){
> $found[$n] =3D $k;
> $n +=3D 1;} }
>
> return $found;
> }
>
> The thing is, I'm getting "The string does NOT equal the string
> [6digit number]." It's not picking up that value from the array...
> hmm......


$V !=3D $v

Please enable display_errors, and set error_reporting to E_ALL | E_STRIC=
T.

And I'm still quite curious what the result of your var_dump was...
-- =

Rik Wasmus