douhe4608 2014-12-17 02:40
浏览 54
已采纳

我该如何循环这个数组

php code:

<?php
$arVal = array('4','2','3','1','6','9','7');
$arLength = count($arVal);

for ($i = 0; $i < $arLength; $i++) {
    $val = '';
    $cVal = '';
    $ar = '';
    // $arNext shift $arVal(array)
    $arNext = next($arVal);
    // $val just contain string $tVal0, $tVal1...
    echo $val = '$tVal'.$i.'=';
    // $ar for loop value inside array 4,2,3,1...
    echo $ar = $arVal[$i].' [';
    // $cVal contain value math = 4+2*(1-4)
    echo $cVal = $ar+$arNext*(1-$ar).'] <br>';
}

i want to loop like this...

$tVal0 = $val0+$val1*(1-$val0);
$tVal1 = $val2+$val0*(1-$val2);
$tVal2 = $val3+$val1*(1-$val3);
$tVal3 = $val4+$val2*(1-$val4);
$tVal4 = $val5+$val3*(1-$val5); 

how to do looping like that (is it possible or not, if not i'm not using loop), look duplicate post then direct me. Thanks.

  • 写回答

1条回答 默认 最新

  • dsg435665475 2014-12-17 03:36
    关注

    If I understand the question correctly then try this. It may not be what you're asking, as I don't understand where $val? and $tVal? is coming from. Are you declaring it, or getting it from the array? And you seem to be assigning a variable after the echo keyword, is that meant to be in quotes?

    echo "$tVal0 = $val0+$val1*(1-$val0)";
      //This one has to be done outside the loop b/c it's different
    
    for($i=1,$i < 5,i++){
      $tVal_Name = 'tVal' . $i;
      $outside_val_name = 'tVal' . $i+1;
      $inside_val_name = 'tVal' . $i-1;
      echo "{$$tVal_Name}={$$outside_val_name}+{$$inside_val_name}*(1-{$$outside_val_name})";
    }
    

    To know more about variable variables, see the php documentation

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据