donglu9898 2015-06-16 23:58
浏览 25
已采纳

如何在一个简单的数组中迭代所有可能的组合[关闭]

Kindly help me with my code:

$a[1]='cloropil';
$a[2]='rodopayta';
$r=2;
$c=1;
$f= while($c<=$r){ echo $a[$c];;
$e = $c++ };
while($c<=$r){
echo $f;
$r++;
}
while($c<=$r){
echo $e;
$r++;
}

I want its output to be like this:

$a[1]='cloropil';
$a[2]='rodopayta';
$r=2;
$c=1;
while($c<=$r){
 echo $a[$c];
   while($c<=$r){
    echo $a[$c];
    $c++;
   }
 $c++;
}

How ever the out put is blank. it should even display at least "cloropil". Is this even possible? I want to display something like this:

cloropilcloropil cloropilrodopyta rodopytacloropil rodopytarodopyta

thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • droe9376 2015-06-17 02:39
    关注

    I think this may be what you are looking for?:

    <?php
    $a[]='cloropil';
    $a[]='rodopayta';
    
        // You first iterate over each
        for($i = 0; $i < count($a); $i++) 
            {
                // As you iterate through the first round, you iterate
                // again to match pairs but include the first iteration
                // value with this second 
                foreach($a as $val) 
                    echo $a[$i].'=>'.$val.'<br />';
            }
    ?>
    

    Gives you:

    cloropil=>cloropil
    cloropil=>rodopayta
    rodopayta=>cloropil
    rodopayta=>rodopayta
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度