PHP:foreach()如何分配给两个变量 [英] php: foreach() how to assign to two variables

查看:188
本文介绍了PHP:foreach()如何分配给两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数示例()
{

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

$ options。='< option value ='。check_plain($ key)。' title ='。$ vtitle。''。 $选择
。'>'。 check_plain($ choice)。'< / option>';


} // vtitle结尾
} //选择结束

return $ options;

下面的一些问题和我想要的
$ b


  1. 数组 $选择不是数字索引。 / li>
  2. 数组 $ vtitle 是数字化的索引。

  3. 它们不会短于彼此因为在代码运行之前,代码会处理这个问题。
  4. 我试图返回 $ options 变量。问题在于 $ choices [0] $ vtitle [0] 只能使用一次。希望我能够表达我的问题。

  5. 对于 $ options
  6. 中的每个值,我都不想一次遍历 $ vtitles


    @hakre:谢谢我几乎已经在您的帮助下解决了问题。



    变量 $ vtitle 出现错误:

      InvalidArgumentException:通过的变量不是数组或对象,使用空数组
    代替ArrayIterator-> __构造()(第35行/ home / vishal / Dropbox / sites / chatter / sites
    /all/themes/kt_vusers/template.php)。

    我确定它是一个数组,这是使用 print_r <$ p
    $ b

      / code> 

    可能会出错?

    ($($ key $,$ value1)= each(

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' $ array $)
    &&(list($ key2,$ value2)= each($ array2))

    {
    printf(%s => %s,%s =>%s \ n,$ key1,$ value1,$ key2,$ value2);


    解决方案

    你用你的伪代码概述。但是,SPL提供了一种迭代多个迭代器的方法。它叫做 MultipleIterator ,你可以附加许多迭代器你喜欢:

      $ multi = new MultipleIterator(); 
    $ multi> attachIterator(new ArrayIterator($ array1));
    $ multi> attachIterator(new ArrayIterator($ array2));
    $ b foreach($ multi as $ value)
    {
    list($ key1,$ key2)= $ multi-> key();
    list($ value1,$ value2)= $ value;

    $ / code>

    请参阅:演示

    编辑:第一个例子显示一个建议来自SPL。它可以处理任何类型的迭代器,而不仅仅是数组。如果你想表达一些与数组相似的东西,你可以用经典的 while(list()= each())循环来实现类似的功能, c $ c> foreach

      while 

    (list( $ key1,$ value1)= each($ array1))
    &&(list($ key2,$ value2)= each($ array2))

    {
    printf(%s =>%s,%s =>%s \\\
    ,$ key1,$ value1,$ key2,$ value2);
    }

    Demo(元素的最小数目被迭代)




    另请参阅相关问题: PHP foreach循环中的多个索引变量


    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

    这篇关于PHP:foreach()如何分配给两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆