TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   can php do this? (http://www.talkphp.com/general/1410-can-php-do.html)

CoryMathews 11-07-2007 11:16 PM

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.

bluesaga 11-08-2007 12:07 AM

Hmm you need to explain your request more.... do you mean something like:

Code:

foreach($x as $y)
 //do something

The above loops through the $x array, making the elements of the array $y.

Andrew 11-08-2007 12:57 AM

Yes, he's looking for a way to do that. Of course it's possible! :)

CoryMathews 11-08-2007 04:16 AM

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...

Andrew 11-08-2007 05:35 AM

Well, you can do something like this:
PHP Code:

$szMatch "hi";
$aArray = array("hi""hello");
foreach (
$aArray AS $szValue) {
      if (
$szValue == $szMatch) {
            echo 
$szValue// Will output "hi"
      
}



Tanax 11-08-2007 06:02 AM

Yea, andrew said the correct way to do that.

In your example it would be:
PHP Code:

// Loops through the array, where $c is the value of the current element
foreach($b as $c) {
// If the current element is equal to the value you want to match
if($c == $a) {
// Do what you want if the match was found
}
else {
// Optional, if the current element was NOT a match to the $a value
}


Also, remember, that if your array consist of 3 elements like this:
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.

bluesaga 11-08-2007 09:51 AM

Tanax, while that is correct it is not the cleanest approach to doing that.

PHP Code:

// Loops through the array, where $c is the value of the current element
foreach($b as $c) {

   if(
$c == $a) {
     break;
   }

   
// Optional, if the current element was NOT a match to the $a value



The above just breaks the loop after its found once, muchos cleaner, if you didn't want to break the loop, you could use "continue;" instead and have it skip the rest of the document that way and go through the next element in the array.

Salathe 11-08-2007 10:53 AM

If you're just searching for the presence of a string, then why not just use in_array?

bluesaga 11-08-2007 11:00 AM

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

CoryMathews 11-08-2007 01:01 PM

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