![]() |
can php do this?
In some languages like python you can just say
for a in b do something with a being a primative and b being a list can this be done in php or do i have to do it the c/java.. way with arrays and all that mess. |
Hmm you need to explain your request more.... do you mean something like:
Code:
foreach($x as $y) |
Yes, he's looking for a way to do that. Of course it's possible! :)
|
ye something like that but in your case its 2 arrays. im more lookin to see if it can do this..
a is a var set to 1 // not an array b is an array of strings. i want it to loop through the array b checking to see if it is equal to a. im guessing it could be done using ur foreach loop there maybe this? foreach($x == $y) // do my command if they are equal. cause im not looking to copy the array just to loop through it and test a condition without having to do the traditional for(int x = 0; x < arrayLen; x++) { ... } this away i dont have to worry about the length or going out of the arrays bounds... |
Well, you can do something like this:
PHP Code:
|
Yea, andrew said the correct way to do that.
In your example it would be: PHP Code:
array('1', '2', '3'); And your $a = 1; the "else" statement will execute all the times the if statement is NOT true. So first time, the if statement will be true, but the 2 other loops, it won't. |
Tanax, while that is correct it is not the cleanest approach to doing that.
PHP Code:
|
If you're just searching for the presence of a string, then why not just use in_array?
|
Ok, ya got me :|
I was trying to just stay away from functions, but i doubt there would be much time difference, and this way you can change the way the array works: stristr() - Match part of word continue - For matching exact word, and then more exact words break - for matching exact word, as in_array would work in the same context |
Thanks guys. thats what i was lookin for.
|
| All times are GMT. The time now is 03:48 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0