codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Sum array values depending dates non-singularity


By : , Category : php

How about this?

$result = $costs;
foreach ($dates as $i => $d) {
    if ($i > 0 && $dates[$i - 1] == $dates[$i]) {
        $result[$i] += $result[$i - 1];
        $result[$i - 1] = 0;
    }
}
print_r($result);

We're starting with our $costs array, and looping through all the dates... every time we detect that the date is the same as the previous entry, we zero out the previous entry and add it's value to our current location.

Alternate Solution

This isn't exactly what you asked for, but I suspect a more useful solution might be this:

$result = array();
foreach ($dates as $i => $d) {
    $result[$d] = (isset($result[$d]) ? $result[$d] : 0) + $costs[$i];
}
print_r($result);

This will produce a result of:

array(
    '2001-01-01' => 5,
    '2001-02-01' => 34,
    '2001-03-01' => 30,
    '2001-04-01' => 16,
    '2001-05-01' => 0
)
ReLated :

If you're iterating over skillslist, each value passed to your callback will be the individual values of skillslist. So if you want to change the values of skillslist, you can do so by changing the first argument passed to your callback. Like this:

skillslist.forEach(function(skill) {
  var isInSkills = skills.some(function(skillValue) {
    return skillValue.Skill_Values__c === skill.Skill_Values__c;
  });
  if(isInSkills) {
    skill.checked = "Added";
  } else {
    skill.checked = "no";
  }
});

Just change inside your htmldata:

ServiceCharge

to:

orderjson.display[orderOfDisplay[i]]

EDIT Then all the vars ServiceCharge, ServiceTax, VAT, OTHERS, DISCOUNT are not needed any more.

words = ['Yes', 'it is', 'done']
prime_arr = [2, 3, 5, 7, 11, 13]
target = [3, 5, 7]

words.take((prime_arr & target).size).join(" ")

Edited:

prime_arr = [2, 3, 5, 7, 11, 13]
mapping = {
  [3] => 'Yes',
  [3, 5] => 'Yes it is',
  [3, 5, 7] => 'Yes it is done'
}

mapping[prime_arr & [3, 5, 7]] # => 'Yes it is done'
mapping[prime_arr & [3, 5]] # => 'Yes it is'
mapping[prime_arr & [3]] # => 'Yes'

You get a nil when none of the keys in mapping matches.

I would suggest to have variable that holds page's index, and then you just include this into each checkbox: <?php if(is_array($categoriesArr[$pageIndexVariable]) && $categoriesArr[$pageIndexVariable][$checkboxNameOrIdOrwhatever]) echo "checked" ?>

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .