1

i have a multidimensional arrary.

Array
(
    [0] => Array
        (
            [NAME] => FEMFAT
            [VERSION] => 5.1
            [VENDOR] => MAGNAECS
            [END] => 2015-01-30
            [USED_LICENSES] => 1
            [TOTAL_LICENSES] => 1
            [DENIED_LICENSES] => 0
        )
[1] => Array
    (
        [NAME] => FEMFAT
        [VERSION] => 5.1
        [VENDOR] => MAGNAECS
        [END] => 2015-01-30
        [USED_LICENSES] => 0
        [TOTAL_LICENSES] => 1
        [DENIED_LICENSES] => 0
    )

from the above array i want the output like below. if name is same remaining values added to first array.

Array
(
    [0] => Array
        (
            [NAME] => FEMFAT
            [VERSION] => 5.1
            [VENDOR] => MAGNAECS
            [END] => 2015-01-30
            [USED_LICENSES] => 1
            [TOTAL_LICENSES] => 2
            [DENIED_LICENSES] => 0
        )

What I've tried:

$newArr = array(); 
foreach ($featureInfo as $val) { 
    $newArr[$val['NAME']] = $val; 
} 
$featureInfo = array_values($newArr);
5
  • 2
    no attempts? foreach loop is a start Commented Oct 16, 2014 at 4:56
  • I think this is pretty straightforward. You can iterate over each array element, compare the first one to the rest, if the name matches, then write conditional statements and do your replacement operations. Commented Oct 16, 2014 at 4:57
  • $newArr = array(); foreach ($featureInfo as $val) { $newArr[$val['NAME']] = $val; } $featureInfo = array_values($newArr); if i tried this way i am geeting unique value but i want to add total_licenses to that value. Commented Oct 16, 2014 at 4:58
  • @Aniket please give me one example. Commented Oct 16, 2014 at 5:00
  • php.net/manual/en/function.array-unique.php Commented Oct 16, 2014 at 5:07

2 Answers 2

2

You're almost there, push the whole array when its not yet set on the new container, if already set, then just add them:

$newArr = [];
foreach($array as $value) {
    if(!isset($newArr[$value['NAME']])) { // if this name is not yet inside, push it
        $newArr[$value['NAME']] = $value;
    } else { // if already inside then add
        $newArr[$value['NAME']]['USED_LICENSES'] += $value['USED_LICENSES'];
        $newArr[$value['NAME']]['TOTAL_LICENSES'] += $value['TOTAL_LICENSES'];
        $newArr[$value['NAME']]['DENIED_LICENSES'] += $value['DENIED_LICENSES'];
    }
}

// $newArr = array_values($newArr); // optional array reindex
Sign up to request clarification or add additional context in comments.

Comments

0

Ghost's answer with array_reduce:

// where $data is your original array
$final = array_values(array_reduce($data, function ($prev, $curr) {
    if (isset($prev[$curr['NAME']])) {
        $prev[$curr['NAME']]['USED_LICENSES'] += $curr['USED_LICENSES'];
        $prev[$curr['NAME']]['TOTAL_LICENSES'] += $curr['TOTAL_LICENSES'];
        $prev[$curr['NAME']]['DENIED_LICENSES'] += $curr['DENIED_LICENSES'];
    } else {
        $prev[$curr['NAME']] = $curr;
    }

    return $prev;
}, array()));

Comments

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.