简体   繁体   中英

php: foreach() how to assign to two variables

function example()
{

    foreach ($choices as $key => $choice) {       # \__ both should run parallel
    foreach ($vtitles as $keystwo => $vtitle) {   # /

        $options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected  
.'>'. check_plain($choice) .'</option>';


    } // end of vtitle    
    } // end of choice

    return $options;
}

Answers to some of the below questions and what I am trying to achieve.

  1. Array $choices is not numerically indexed.
  2. Array $vtitle is numerically indexed.
  3. They won't be shorter than each other as I have code which will take care of this before this code runs.
  4. I am trying to return $options variable. The issue is that $choices[0] and $vtitle[0] should be used only once. Hope I was able to express my problem.
  5. I do not want to go through the $vtitles array once for each value in $choices .

@hakre: thanks I have nearly solved it with your help.

I am getting an error for variable $vtitle :

InvalidArgumentException: Passed variable is not an array or object, using empty array    
instead in ArrayIterator->__construct() (line 35 of /home/vishal/Dropbox/sites/chatter/sites
/all/themes/kt_vusers/template.php).

I am sure its an array this is the output using print_r

Array ( [0] => vishalkh [1] => newandold ) 

What might be going wrong ?

The below worked for me , thank you hakre

while
(
(list($key1, $value1) = each($array1))
&& (list($key2, $value2) = each($array2))
)
{
printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
}

It does not work the way you outline with your pseudo code. However, the SPL offers a way to iterate multiple iterators at once. It's called MultipleIterator and you can attach as many iterators as you like:

$multi = new MultipleIterator();
$multi->attachIterator(new ArrayIterator($array1));
$multi->attachIterator(new ArrayIterator($array2));

foreach($multi as $value)
{
    list($key1, $key2) = $multi->key();
    list($value1, $value2) = $value;
}

See it in action: Demo

Edit: The first example shows a suggestion from the SPL. It has the benefit that it can deal with any kind of iterators, not only arrays. If you want to express something similar with arrays, you can achieve something similar with the classic while(list()=each()) loop, which allows more expressions than foreach .

while
(
    (list($key1, $value1) = each($array1))
    && (list($key2, $value2) = each($array2))
)
{
    printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
}

Demo (the minimum number of elements are iterated)


See as well a related question: Multiple index variables in PHP foreach loop

you can't do it in foreach loop in general case. But you can do it like this in PHP5:

$obj1 = new ArrayObject($arr1);
$it1 = $obj1->getIterator();

$obj2 = new ArrayObject($arr2);
$it2 = $obj2->getIterator();

while( $it1->valid() && $it2->valid())
{
    echo $it1->key() . "=" . $it1->current() . "\n";
    $it1->next();

    echo $it2->key() . "=" . $it2->current() . "\n";
    $it2->next();    
}

In older versions of PHP it will be like this:

while (($choice = current($choices)) && ($vtitle = current($vtitles))) {       
    $key_choice = key($choices);
    $key_vtitle = key($vtitles);
    next($choices);
    next($vtitles);
}

Short Answer, no.

What you can do instead is:

foreach ($array1 as $k => $v)
    performMyLogic($k, $v);

foreach ($array2 as $k => $v)
    performMyLogic($k, $v);

You may have to refigure your logic to do what you want. Any reason you can't just use 2 foreach loops? Or nest one inside the other?

EDIT: as others have pointed out, if PHP did support what you're trying to do, it wouldn't be safe at all.. so it's probably a good thing that it doesn't :)

If the keys are the same, you can do this:

foreach ($choices as $key => $choice) {
    $vtitle = $vtitles[$key];
}

Maybe you want this:

//get theirs keys
$keys_choices = array_keys($choices);
$keys_vtitles = array_keys($vtitles);

//the same size I guess
if (count($keys_choices) === count($keys_vtitles)) {

    for ($i = 0;$i < count($keys_choices); $i++) {

        //First array
        $key_1 = $keys_choices[$i];
        $value_1 = $choices[$keys_choices[$i]];

        //second_array
        $key_2 = $key_vtitles[$i];
        $value_2 = $vtitles[$keys_vtitles[$i]];

        //and you can operate
    }
}

But this will work if size is the same. But you can change this code.

You need to use two foreach loops.

foreach ($choices as $keyChoice => $choice)
{
    foreach ($vtitles as $keyVtitle => $vtitle)
    {
        //Code to work on values here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM