【问题标题】:Extract values from arrays using php [closed]使用php从数组中提取值[关闭]
【发布时间】:2016-03-03 19:23:29
【问题描述】:

我有一组包含以下详细信息的数组:

$thismemberinfo[0]  to $thismemberinfo[7]
$offercurrent[]  = emtpy , will get it values from $thismemberinfo[]
$offerpoints[0] to $offerpoints[7]
$offerpercent[] = emtpy , will get its value from the formula

我正在手动填写值。

$offercurrent[0]  = $thismemberinfo[0];
$per      = $offercurrent[0]/$offerpoints[0] * 100;
$offerpercent[0] = round($per);


$offercurrent[1]  = $thismemberinfo[1];
$per      = $offercurrent[1]/$offerpoints[1] * 100;
$offerpercent[1] = round($per);
// and so on

问题:

我想自动化这个过程。我试过这种方法但不起作用:

for ($i=0;$i<=7;$i++) {
 $offercurrent[i]  = $thismemberinfo[i];
 $per      = $offercurrent[i]/$offerpoints[i] * 100;
 $offerpercent[i] = round($per);
}

【问题讨论】:

  • $offerpercent[$i] $i,看到了吗?
  • 您还应该在除法中使用$offerpoints[i] 之前对其进行验证。
  • $u_mulder,你说得对,如果你喜欢,请把它作为答案,我会接受它

标签: php for-loop foreach


【解决方案1】:

看起来您从i 变量中遗漏了$$i。试试下面的。如果该数字是动态的,您还可以使用 $i&lt;=count($offercurrent) 而不是 $i&lt;=7

for ($i=0;$i<=7;$i++) {
    $offercurrent[$i]  = $thismemberinfo[$i];
    $per      = $offercurrent[$i]/$offerpoints[$i] * 100;
    $offerpercent[$i] = round($per);
}

【讨论】:

  • 是的,我错过了 $ ,它运作良好
猜你喜欢
  • 2013-03-11
  • 2023-03-30
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多