循环通过动态命名的数组并将结果与先前的位置结果进行比较.
if($s>1 && $s<=10)
{
if( ${"strat{$s}"}[total] > ${"strat{$s-1}"}[total] )
$sl_best = $sl_mult; //if this one did better than the previous one, then grab the value
}
Run Code Online (Sandbox Code Playgroud)
而且我收到与之相关的错误消息${"strat{$s-1}"}[total],特别是{$s-1}部分.这是错误消息:
Parse error: syntax error, unexpected '-', expecting '}' ...
Run Code Online (Sandbox Code Playgroud)
有关如何检查动态命名变量上的前一个数组位置的任何想法?
一个解决办法我只好用$previous = $s-1;每张支票前,然后取代$strat[$previous]了${"strat{$s-1}"},但是这似乎难看,我想看看是否有人有一个更好的解决方案吗?
应始终避免使用动态变量,而应尝试使用其他数组
样本数组
$strat = array(
array('total' => 'some val'),
array('total' => 'some val2'),
array('total' => 'some val3'),
array('total' => 'some val4'),
...
);
Run Code Online (Sandbox Code Playgroud)
然后
if($s>1 && $s<=10) {
if($strat[$s]['total'] > $strat[$s-1]['total']) {
$sl_best = $sl_mult; //if this one did better than the previous one, then grab the value
}
}
Run Code Online (Sandbox Code Playgroud)