0

I am trying to parse an array of strings with irrelevant leading text (to be removed) then create an associative array where values are summed if the same key is encountered more than once.

$assessmentArr = [
    'a_b_money-rewards=30',
    'c_d_status-stability=15',
    'e_f_personal-professional-development=32',
    'g_h_community-support=9',
    'i_j_money-rewards=12',
    'k_l_status-stability=16',
    'm_n_personal-professional-development=29',
];

foreach($assessmentArr as $data) {
    $fullArr = explode("_", $data);
    $resultArr = explode("=", $fullArr[2]);
    // what do I do with $resultArr?
}

Desired result:

Array
(
    [community-support] => 33
    [money-rewards] => 42
    [status-stability] => 31
    [personal-professional-development] => 61
)

I found this question: How to merge two arrays by summing the merged values which will assist me in merging and adding the values together, but I'm not sure how to go about it when the arrays aren't assigned to a variable. Is what I am trying to do possible or am I going about this the wrong way?

3

3 Answers 3

2

Don't make it complicated, just check if the results array already has an element with that key and if not initialize it otherwise add it. E.g.

(Add this code in your loop):

if(!isset($result[$resultArr[0]]))
    $result[$resultArr[0]] = $resultArr[1];
else
    $result[$resultArr[0]] += $resultArr[1];

Then you have your desired array:

print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

0

You could do it like this

$assessmentArr = explode("&", $assessmentData);
$finalArr = array();
foreach($assessmentArr as $data) {
    $fullArr = explode("_", $data);

    // Break down to only archetype and value
    $resultArr = explode("=", $fullArr[2]);
    if(array_key_exists($resultArr[1], $finalArr)){
        $finalArr[$resultArr[0]] += $resultArr[1];
    }else{
        $finalArr[$resultArr[0]] = $resultArr[1];
    }
}

First check, if the key already exists in the array, if so you add the value to the value in the final array. Otherwise you add the new index to the final array, with the value from resultArr as inital value.

... way too slow :/

Comments

0

Upon parsing the text into its meaningful parts, use the hyphenated strings as keys and the numbers as values. If a key is encountered for the first time, store the sum of 0 and the current value; otherwise sum and store the stored value and the current value.

Code: (Demo)

$result = [];
foreach (explode("&", $assessmentData) as $data) {
    sscanf(preg_replace('/.*_/', '', $data), '%[^=]=%d', $k, $v);
    $result[$k] = ($result[$k] ?? 0) + $v;
}
var_export($result);

Or for more direct parsing, use a preg_match_all() call. Demo

$result = [];
preg_match_all('/[^_]*_[^_]*_([^=]+)=(\d+)/', $assessmentData, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
    $result[$m[1]] = ($result[$m[1]] ?? 0) + $m[2];
}
var_export($result);

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.