still problems with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

still problems with arrays

Post by pelegk2 »

1i have this array :

Code: Select all

<?php
$lstOpenRounds ===
Array
(
    [0] => 006
    [1] => 97
    [2] => 1
    [3] => 012
    [4] => 162
    [5] => 1
    [6] => 012
    [7] => 163
    [8] => 1
    [9] => 019
    [10] => 160
    [11] => 2
    [12] => 019
    [13] => 158
    [14] => 1
)
?>
and i do :

Code: Select all

<?php
array_search ($row_orders_round['region_id'],$lstOpenRounds); 


?>
where $row_orders_round['region_id']=="002"
and i recive the 11 inde!!!!!
but in 11 index its "2" and not "002"
what is going on here?
thnaks i nadvane
peleg
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

That's because '002' == '2' but it doesn't === 2.
So use the third parameter to array_search which makes it also check the type.

Code: Select all

array_search ($row_orders_round['region_id'],$lstOpenRounds, TRUE);
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

but i built the array

Post by pelegk2 »

as an array with string elements like this :
while($row=mysql_fetch_array($result)){
array_push($1stOpenRounds,"".$row['myVal1']);
array_push($1stOpenRounds,"".$row['myVal2']);
array_push($1stOpenRounds,"".$row['myVal3']);

}
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Right, but you're in effect trying to match '2' with 2
So while '2' == 2 , '2' !== 2 i.e the values are the same but the types differ.
Did you try using the third argument to array_search() ?
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

well i am not at work a t the moment but i can tell u for sure that i tried to compare "002" with "002"
does the way i push values into the array dosent make them string?
is there a way to convert int into a string?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Code: Select all

<?php
$foo = (int) $foo;
$bar = (string) $bar;
?>
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Maybe this one works:

Code: Select all

<?php

array_search (".$row_orders_round['region_id'].",$lstOpenRounds); 

?>
Post Reply