-1

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:

enter image description here

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}

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?

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100
  • Isn't the last value missing in the $start_point array? Did you try a loop? What is dynamic means? – Micromega Nov 26 '13 at 02:37
  • 4 lines = 4 starting points. A loop would be necessary to fill the starting points array, but I'm not sure how to construct the loop to create the new array and the starting points. Dynamic means the algorithm needs to be flexible to work different values in the database...which a loop would of course handle – IlludiumPu36 Nov 26 '13 at 02:57
  • No, it's a real question...do you have an answer? – IlludiumPu36 Nov 26 '13 at 03:17

3 Answers3

1

I think this is a

perfect opportunity

to use array_map:

<?php

$seg = array(50, 75, 20, 90);
$start = 100; // offset

$pts = array_map(function($n) use (&$start) {
    $val = $start;
    $start += $n;
    return $val;
},$seg);

print_r($pts);

?>

print_r provides the

output desired with the one-

hundred offset too:

Array
(
    [0] => 100
    [1] => 150
    [2] => 225
    [3] => 245
)

This is poetry

and code art for someone's eyes

using array_map.

zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • oo.. this is the much better answer if we're going for simplicity :) – haxxxton Nov 26 '13 at 06:58
  • 1
    mine is slower!! 0.36s avg (denkiryokuhatsuden) vs 1.10s avg (zamnuts) 5 x 100000 iterations. cry... but i still think it reads better! – zamnuts Nov 26 '13 at 13:29
  • and i think 'technically' he did specify that he wanted to use a loop. Not sure if that was really a necessity or just a turn of phrase though :) – haxxxton Nov 27 '13 at 00:17
  • @haxxxton if it is a passive refactor then it doesn't matter what the solution is, as long as it does the job – zamnuts Nov 27 '13 at 13:26
  • 1
    i agree, and i think clean, more beautiful code (even if its a slight performance hit) should always triumph <3 Was more playing devils advocate for why OP might not select this as the 'correct' answer :) – haxxxton Nov 28 '13 at 14:03
  • I just wrote according to what Peter said :) Yes, if I'm to refactor my code, it would be like one that zamnuts wrote. Loop shows only it iterates, but array_map exactly shows that "The code wants to map one array to another". – denkiryokuhatsuden Dec 09 '13 at 04:25
0

I figured a single multi dimension array should be easier than trying to reference 2 arrays later. This will also allow you to reference the length of the last item in the length array from the one array :)

// $dna_segment_length = {50, 75, 20, 90}
$starting_int = 100; // the starting value for the line distance
$prev_val = 0; // initialised and set to 0 so we dont effect $starting_int
$segment_array = array(); // will be our reference multi dimension array
for($x = 0; $x < count($dna_segment_length); $x++){
    $segment = array(); // local array
    $segment["start"] = $starting_int + $prev_val;
    $segment["length"] = $dna_segment_length[$x];
    array_push($segment_array, $segment);
    //set values for next iteration
    $starting_int .=  $prev_val;
    $prev_val = $dna_segment_length[$x];
}

// output array
/* {
    [0]=> array(
        ["start"] => 100,
        ["length"] => 50
    ),
    [1]=> array(
        ["start"] => 150,
        ["length"] => 75
    ),
    [2]=> array(
        ["start"] => 225,
        ["length"] => 20
    ),
    [3]=> array(
        ["start"] => 245,
        ["length"] => 90
    ),
    }
*/
haxxxton
  • 6,422
  • 3
  • 27
  • 57
  • I liked your idea of the multidimensional array, but was really after something simpler. Thanks very much with your help though! Still don't understand why my question was downvoted in the first place... – IlludiumPu36 Nov 26 '13 at 04:52
0
//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];
}