php - checking previous with dynamic variable names
Looping through a dynamically-named array and comparing results to the previous positions results.
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
}
And I'm getting error messages related the the ${"strat{$s-1}"}[total]
, specifically the {$s-1}
portion. Here is the error message:
Parse error: syntax error, unexpected '-', expecting '}' ...
Any thoughts on how to check the previous array position on a dynamically named variable?
One solution I had was with 开发者_如何学JAVA$previous = $s-1;
preceding every check and then substituting $strat[$previous]
for ${"strat{$s-1}"}
, but this seems ugly and I wanted to see if anyone had a better solution?
Dynamic variables should always be avoided, instead try using another array
Sample array
$strat = array(
array('total' => 'some val'),
array('total' => 'some val2'),
array('total' => 'some val3'),
array('total' => 'some val4'),
...
);
then
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
}
}
精彩评论