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 02-07-2010, 03:08 AM   #1 (permalink)
The Wanderer
 
Join Date: Feb 2010
Posts: 6
Thanks: 1
aquastream is on a distinguished road
Default Inconsistencies in foreach Working

Here's another newbie question which I'm confused about. I've used the foreach function to list the contents of the first array. However, on the second array I can't get the same results. I simply get the last result echoed and the other ones are ignored.

<?php
$array1 = array("Lisa" => 28,"Jack" => 16,"Ryan" => 35,"Rachel" => 46,"Grace" => 34);
foreach($array1 as $names => $numbers)
echo "Name: $names, Age: $numbers <br />"
?>
<br />
<?php
$beatles = array("Singer 1" => "Paul","Singer 2" => "John","Guitarist" => "George","Drummer" => "Ringo");
foreach($beatles as $roles => $names);
echo "$roles: $names <br />"
?>

Can anyone see what I've done wrong here?
Thanks
aquastream is offline  
Reply With Quote
Old 02-07-2010, 04:27 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

php Code:
foreach($beatles as $roles => $names); // Notice the semi-colon?
echo "$roles: $names <br />" // This isn't inside of a loop

// Try this:
foreach ( $beatles as $roles => $names ) {
    echo "$roles: $names<br />";
}

Even though PHP allows you to omit braces in one line situations, it's good to get into the practice of using them anyways.
delayedinsanity is offline  
Reply With Quote
Old 02-07-2010, 05:14 AM   #3 (permalink)
The Wanderer
 
Join Date: Feb 2010
Posts: 6
Thanks: 1
aquastream is on a distinguished road
Default

Thanks again delayedinsanity. Again it was something rather simple, but it did the job.
aquastream 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
foreach statement? Randy Absolute Beginners 6 09-10-2009 07:14 PM
Wamp working, but what next? Help! DotNetTim Absolute Beginners 3 02-07-2009 10:05 AM
Save output from foreach into an array Sam Granger Absolute Beginners 2 01-15-2009 02:07 AM
if within a foreach ? Mithadriel Absolute Beginners 2 11-09-2008 12:20 AM


All times are GMT. The time now is 03:57 PM.

 
     

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