doushua7737 2010-11-17 19:35
浏览 46
已采纳

如何将php数组的元素相乘

I'm trying to translate a javascript function into php but having some problems with my arrays. I need to iterate over the array elements, multiplying them all by a certain amount, but it's not changing the values. Pretty sure it's because my syntax $coordinates_p[i][0] *= $scale; isn't correct, but I'm not sure what it should be!

Test code:

<?php

print "Starting.<br/>";

$scale = 100;

$coordinates_p = array();

$i = 0;
$x_coordinate = 1;
$y_coordinate = 2;
while ($i <= 1) {
    $coordinates_p[$i] = array(0 => $x_coordinate, 1 => $y_coordinate);
    $x_coordinate += 1;
    $y_coordinate += 2;
    $i++;
}

print "Unscaled: ";
print_r ($coordinates_p);
print "<br/>";

$i = 0;
while (isset($coordinates_p[i])) {
    $coordinates_p[i][0] *= $scale;
    $coordinates_p[i][1] *= $scale;
    $i++;
}

print "Scaled: ";
print_r ($coordinates_p);
print "<br/>";

print "Finished.";

?>
  • 写回答

3条回答 默认 最新

  • dongquan8753 2010-11-17 19:39
    关注

    Your code just needs to change from

    $coordinates_p[i][0] *= $scale;
    $coordinates_p[i][1] *= $scale;
    

    to

    $coordinates_p[$i][0] *= $scale;
    $coordinates_p[$i][1] *= $scale;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划