TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-07-2007, 10:16 PM   #1 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default 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.
CoryMathews is offline  
Reply With Quote
Old 11-07-2007, 11:07 PM   #2 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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.
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-07-2007, 11:57 PM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Yes, he's looking for a way to do that. Of course it's possible! :)
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-08-2007, 03:16 AM   #4 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

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...
CoryMathews is offline  
Reply With Quote
Old 11-08-2007, 04:35 AM   #5 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

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"
      
}

Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-08-2007, 05:02 AM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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.
Tanax is offline  
Reply With Quote
Old 11-08-2007, 08:51 AM   #7 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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.
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-08-2007, 09:53 AM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

If you're just searching for the presence of a string, then why not just use in_array?
Salathe is online now  
Reply With Quote
Old 11-08-2007, 10:00 AM   #9 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-08-2007, 12:01 PM   #10 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Thanks guys. thats what i was lookin for.
CoryMathews is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design