根据另一个数字数组计算新的数组值 [英] Calculating new array values based on another numeric array

查看:82
本文介绍了根据另一个数字数组计算新的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组(从数据库填充) $ dna_segment_length ,在这种情况下:

I have an array (populated from a database) $dna_segment_length, in this case:

{50, 75, 20, 90}

每个值代表一条线长度。这些线条在画布上绘制如下:

Each value represents a line length. The lines are being drawn on canvas like:

为了使线段连接成一条连续的水平线,我需要创建另一个数组,比如 $ start_points ,表示每行的起点。在上面的示例中,此数组将如下所示,第一行设置起始点:

To make the line segments join into a continuous horizontal line, I need to create another array, say $start_points, which represent the starting points for each line. In the above example, this array would be like the following with a set starting point for the first line:

{100, 150, 225, 245}

起始点数组中的第一个值总是需要为100.连续值是通过在新数组中添加行长度和前一个值来创建的。问题是我应该如何使用循环?

The first value in the starting points array always needs to be 100. Successive values are created by adding the line length and the previous value in the new array. The question is how should I do this using a loop?

推荐答案

//The first value in the starting points array always needs to be 100
$firstValue = 100;

$start_points = array();
$currentValue = $firstValue;
$dnaSegmentLengthCount = count($dna_segment_length);

//using a loop
for ($i = 0; $i < $dnaSegmentLengthCount; ++$i) {
    $start_points[$i] = $currentValue;

    //created by adding the line length and the previous value in the new array
    $currentValue += $dna_segment_length[$i];
}

这篇关于根据另一个数字数组计算新的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆