Chappers Posted January 2, 2007 Share Posted January 2, 2007 Hi, don't know if I've confused terms in this topic title?I've got several arrays and I wish to use the foreach function on each one, but rather than coding this:[code]foreach ($array1 as $name) {echo $name;}foreach ($array2 as $age) {echo $age;}[/code]I was wondering if I can put both into just one foreach function, kind of like:[code]foreach ($array1 as $name, array2 as $age) {echo $name;echo $age;}[/code]But writing it like that won't work, and Googling hasn't brought me any answers, maybe as I'm using the wrong terminology for the different parts of a function and the name for whatever is inside the () brackets.Thanks, James Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 2, 2007 Share Posted January 2, 2007 For each can only loop through one array/array level at a time. It can not loop through multiple arrays/levels. Post what your arrays look like here. Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2007 Share Posted January 2, 2007 Have you tried reading http://php.net/foreach ? Quote Link to comment Share on other sites More sharing options...
Chappers Posted January 2, 2007 Author Share Posted January 2, 2007 Arrays are set like this:[code]$array1 = array('andrew', 'dave', 'charlie', 'peter', 'susan', 'greg');$array2 = array('18', '27', '17', '43', '32', '21');[/code] Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 2, 2007 Share Posted January 2, 2007 is array two the ages? of the names in array1?You may aswel the join the two instead. so its like this:$array('name' => 'age', 'name' => age)That way it will be easier loop through in a foreach loop. rather than handling two different arrays. Like so:[code=php:0]$array1 = array('andrew', 'dave', 'charlie', 'peter', 'susan', 'greg');$array2 = array('18', '27', '17', '43', '32', '21');// use this for our internal counter in the loo]$i = 0;foreach($array1 as $name){ // people is the array we are going to create // here we are going array1 with array2 // array1 is going to be the key eg myarray['keyname'] // array2 is going to be the value // first item will be $people['andrew'] = 18; $people[$name] = $array2[$i]; $i++;}echo '<pre>' . print_r($people, true) . '</pre>';[/code] Quote Link to comment Share on other sites More sharing options...
Chappers Posted January 2, 2007 Author Share Posted January 2, 2007 Thanks for that idea. Only problem is that making the array like:[code]$array1 = array('andrew' => '21', 'dave' => '30');foreach ($array1 as $name => $age)echo "user {$name} is {$age} years old.";[/code]is fine with only the two items (name and age, as above), but if I add another entry, like:[code]$array1 = array('andrew' => '21' => 'UK', 'dave' => '30' => 'USA');foreach ($array1 as $name => $age => $location)echo "user {$name} is {$age} years old and from {$location}.";[/code]I get a blank page. Is this due to only being able to put two fields in the form of blah => bluh, etc., or something? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 2, 2007 Share Posted January 2, 2007 You can still do that but the parameter for the foreach loop is wrong:$array1 as $name => $age => $locationThe parameter for the foreach loop can only be either of the following:foreach($array as $value)orforeach($array as $key => $value)The first will be ideal if the array didnt have any keys assigned, whereas the latter one would.What you are best of doing is something like this for the array format:array('name' => array(age, 'location'))so it'll be like this:[code]$myvar = array('andrew' => array(18, 'USA'));[/code]You will need to design how the array will be, then when you've done that you can the set about doing the foreach. So if you go for the newer array you'll do this:[code]// the people array$people['andrew'] = array(18, 'USA');$people['dave'] = array(27, 'DEN');$people['charlie'] = array(17, 'GBR');foreach($people as $name => $details){ echo $name . ' is from ' . $details[1] . ' and is aged ' . $details[0] . "<br />\n";}[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 2, 2007 Share Posted January 2, 2007 Well, you can't do that... That means key => value.Maybe make a class, then an array of instances of it and loop through there.Or for a simpler version,[code]$person = ('name'=>'andrew', 'age'=>21, 'loc'=>'UK');$people[] = $person;$person = ('name'=>'joe', 'age'=>23, 'loc'=>'USA');$people[] = $person;foreach($people AS $person){ $age = $person['age']; $name = $person['name']; //etc...}[/code] Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted January 2, 2007 Share Posted January 2, 2007 what is wrong with wildteen's version, its nicely constructed and a lot more neater. i'd go with wildteens. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 2, 2007 Share Posted January 2, 2007 I wrote mine while he was writing his. There isn't anything wrong with it, but I posted another way. The construction is a personal preference for the author. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 3, 2007 Share Posted January 3, 2007 I too would prefer Wildteen's method with the array keys.But not only that, his works while yours doesn't. You forgot the "array" bits$person = [color=red]array[/color]('name'=>'andrew', 'age'=>21, 'loc'=>'UK');$people[] = $person;$person = [color=red]array[/color]('name'=>'joe', 'age'=>23, 'loc'=>'USA');$people[] = $person; Quote Link to comment Share on other sites More sharing options...
Chappers Posted January 3, 2007 Author Share Posted January 3, 2007 Thanks to all for your help and advice, I now have it working the way I want. So many things I couldn't do if not for the help I get in this forum - just so you know it's always appreciated and really makes my day because there's nothing worse than learning something, seeing it start to work, then finding a glitch that just boggles you.There was something else I didn't understand properly, but I can't recall what it was now!Thanks again,James Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.