Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Foreach with multiple statements?
Old 11-29-2010, 08:11 PM Foreach with multiple statements?
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hello,

I'm making something pretty cool here, but I've come across this problem. I need to have a foreach that will support 2 variables... let me show you .

PHP Code:
foreach ($paths as $path) {
  
//...blah blah blah
}

//What I want to do:

foreach ($paths as $path && $rects as $rect) {
  
//...blah blah blah with $path and $rect

Is that possible? So far I've gotten just errors, and I don't want to nest them because that'll create a weird sort of glitchy loop.

Thanks!

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-29-2010, 08:31 PM Re: Foreach with multiple statements?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
PHP Code:
$count max(count($paths), count($rects));

for(
$i 0$i $count$i++)
{
     if(isset(
$rects[$i]))
     {

     }

     if(isset(
$paths[$i]))
     {

     }

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-04-2010, 04:25 AM Re: Foreach with multiple statements?
Junior Talker

Posts: 3
Trades: 0
Or you could mix the 2 arrays into one like that:

PHP Code:
$new_array = array($paths$rects); 
Then:

PHP Code:
foreach($new_array as $item)
{
        
//do stuff with  $item[0]
        //do stuff with  $item[1]

__________________
I deal with
Please login or register to view this content. Registration is FREE
.
eugen_r2 is offline
Reply With Quote
View Public Profile
 
Old 12-04-2010, 12:52 PM Re: Foreach with multiple statements?
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
I will try that, thank you eugen!

And NullPointer, thank you for your solution, sorry I didn't get back to you sooner It works well, btw
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Foreach with multiple statements?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25946 seconds with 12 queries