0

I am trying to make a function where I get data from specific positions in an array, and then add(plus) the results together. Something like this:

$specificPositionsInArray = "1,4,12,27,40,42,48,49,52,53,56,58";    
$dataArray = "1,2,3,4,5,6,7,8";  // More than hundred values.

myfunction($specificPositionsInArray) {    
    // Find position in array, based on $specificPositionsInArray and then
    // plus the value with the previous value
    // that is found in the $specificPositionsInArray.

    // Something like:     
    $value = $value + $newValue;

    return $value;
}

So if $specificPositionsInArray was 1,3,5

The $value that should be returned would be: 9 (1+3+5) (based on the $dataArray).

Maybe there is another way to do it, like without the function.

3
  • so you want to add $dataArray[$specificPositionsInArray] to $dataArray[$specificPositionsInArray] ? Commented Apr 22, 2014 at 19:29
  • I want to find a value in $dataArray, store that value (A), then find another value in $dataArray, store that value (B), and then (A)+(B). Commented Apr 22, 2014 at 19:31
  • $dataArray = "1,2,3,4,5,6,7,8"; is not an array, do you mean $dataArray = [1,2,3,4,5,6,7,8];? Commented Apr 22, 2014 at 19:32

4 Answers 4

3

Here's a functional approach:

$specificPositionsInArray = array(1,3,7,6);    
$dataArray = array(1,2,3,4,5,6,7,8);

function totalFromArrays($specificPositionsInArray, $dataArray) {    
    foreach ($specificPositionsInArray as $s){
        $total += $dataArray[$s];
    }

    return $total;
}


$total = totalFromArrays($specificPositionsInArray, $dataArray);
echo $total;
Sign up to request clarification or add additional context in comments.

Comments

0

You should look into arrays and how to handle them, you could have found the solution pretty easily. http://www.php.net/manual/en/book.array.php

//$specificPositionsInArray = array(1,4,12,27,40,42,48,49,52,53,56,58);
$specificPositionsInArray = array(1,3,5);
$dataArray = array(1,2,3,4,5,6,7,8);

$total=0;
foreach($specificPositionsInArray as $k => $v){
    $total += $dataArray[$v-1];
}
echo $total;

The weird part about this is the $v-1 but because of how you want to handle the addition of the items, and an array starts with element 0, you have to subtract 1 to get to the right value.

Comments

0

So you want to do something like this:

$dataArray = array(1,2,3,4,5...);  // 100+ values, not necessarily all integers or in ascending order

$specificPositions = array(1, 3, 5);

function addSpecificPositions($data, $positions) {
    $sum = 0;
    foreach($positions as $p)
        $sum += $data[$p];
    return $sum;
}

If you really want to keep your list of values in a string (like you have it in your example), you'll have to do an explode first to get them in array form.

Comments

0

Since it looks like your array is using numeric values for the keys this should be fairly easy to calculate. I refactored your code a little to make it easier to read:

$specificPositionsInArray = array(1,4,6,7);

By default PHP will assign numeric keys to each value in your array, so it will look like this to the interpreter.

array(
    [0] => 1,
    [1] => 4,
    [2] => 6,
    [3] => 7
)

This is the same for all arrays unless you specify a numeric or mixed key. Since the data array seems to just be values, too, and no keys are specified, you can simply target them with the key that they will be associated with. Say I use your array example:

$dataArray = array(1,2,3,4,5,6,7,8);

This will look like this to the parser:

array(
    [0] => 1,
    [1] => 2,
    [2] => 3,
    [3] => 4,
    [4] => 5,
    [5] => 6,
    [6] => 7,
    [7] => 8
)

If you wanted to select the number 6 from this array, you actually need to use $dataArray[5] since array keys start at zero. So for your function you would do this:

$specificPositionsInArray = array(1,4,6,7);
$dataArray = array(1,2,3,4,5,6,7,8);

calculate_array($specificPositionsInArray, $dataArray); // Returns 18

function calculate_array($keys, $data){
    $final_value = 0; // Set final value to 0 to start

    // Loop through values
    foreach($keys as $key){
        // Add the keys to our starting value
        $final_value += $data[$key-1]; // minus 1 from key so that key position is human readable
    }

    // return the sum of the values
    return $final_value;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.