1

I'm trying to sum value of array and check if value <= less than or equal to 10 then add new index key with value:

source array:

$arr= array(
    'all' => array(
        'db1' => array(
            'val' => 9.4,
            'ran' => 'ex'
        ),
        'db2' => array(
            'val' => 5.2,
            'ran' => 'as'
        ),
        'db3' => array(
            'val' => 2.5,
            'ran' => 'bm'
        ),
        'db4' => array(
            'val' => 7.4,
            'ran' => 'kl'
        ),
        'db5' => array(
            'val' => 2.9,
            'ran' => 'df'
        )
    )
);

result array:

$res = array(
    all => array(
        'db1' => array(
            'val' => 9.4,
            'ran' => 'ex',
            'index' => 1
        ),
        'db2' => array(
            'val' => 5.2,
            'ran' => 'as',
            'index' => 2
        ),
        'db3' => array(
            'val' => 2.5,
            'ran' => 'bm',
            'index' => 2
        ),
        'db4' => array(
            'val' => 7.1,
            'ran' => 'kl',
            'index' => 3
        ),
        'db5' => array(
            'val' => 2.9,
            'ran' => 'df',
            'index' => 3
        )
    )
);

as we can see db1['val'] can't sum with db2['val'] because result will be greater than 10 so add index 1, db2 and db3 is less than 10 so add same index 2 to both of them.

any help or suggestions would be appreciated.

2
  • What have you tried so far? I don't see any code Commented Jul 28, 2019 at 11:10
  • Possibly related, as this appears to be an XY issue: stackoverflow.com/questions/14129023/… Commented Jul 28, 2019 at 11:18

1 Answer 1

3

Well, you need to walk through the array elements and keep track of your index and sum:

<?php
$data = [
    'db1' => ['val' => 9.4, 'ran' => 'ex'],
    'db2' => ['val' => 5.2, 'ran' => 'as'],
    'db3' => ['val' => 2.5, 'ran' => 'bm'],
    'db4' => ['val' => 7.4, 'ran' => 'kl'],
    'db5' => ['val' => 2.9, 'ran' => 'df']
];

$sum = 0;
$index = 1;
array_walk($data, function(&$entry) use (&$sum, &$index) {
    $sum += $entry['val'];
    if ($sum >= 10) {
        $index++;
        $sum -= 10;
    }
    $entry['index'] = $index;
});

print_r($data);

This variant would even be a bit more compact and also cleaner, but harder to read and a bit risky:

<?php
$data = [
    'db1' => ['val' => 9.4, 'ran' => 'ex'],
    'db2' => ['val' => 5.2, 'ran' => 'as'],
    'db3' => ['val' => 2.5, 'ran' => 'bm'],
    'db4' => ['val' => 7.4, 'ran' => 'kl'],
    'db5' => ['val' => 2.9, 'ran' => 'df']
];

array_walk($data, function(&$entry) use (&$sum, &$index) {
    $sum += $entry['val'];
    if ($sum >= 10) {
        $index++;
        $sum -= 10;
    }
    $entry['index'] = $index + 1;
});

print_r($data);

The obvious output is:

Array
(
    [db1] => Array
        (
            [val] => 9.4
            [ran] => ex
            [index] => 1
        )

    [db2] => Array
        (
            [val] => 5.2
            [ran] => as
            [index] => 2
        )

    [db3] => Array
        (
            [val] => 2.5
            [ran] => bm
            [index] => 2
        )

    [db4] => Array
        (
            [val] => 7.4
            [ran] => kl
            [index] => 3
        )

    [db5] => Array
        (
            [val] => 2.9
            [ran] => df
            [index] => 3
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

this heal my headache over 2 days, will read more about array_walk.. thank you!
The important point here is not the array_waslk(...) function, but how to keep track of sum and index. You can do the same using a simple for or foreach loop, but above is easier to read in my eyes, since it clearly encapsulates the process without poluting the calling functions name space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.