Jump to content

[SOLVED] Can I make foreach function run multiple parameters?


Chappers

Recommended Posts

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

You can still do that but the parameter for the foreach loop is wrong:
$array1 as $name => $age => $location

The parameter for the foreach loop can only be either of the following:
foreach($array as $value)
or
foreach($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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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;
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.