addition of strings inside an multidimensional array - php
Get the solution ↓↓↓I have an array like this. Each array is having a user ID with scores populated dynamically. I want to add all the strings inside the array and have the key as userID
array(3) {
[13702]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "9"
[2]=> .....
..... more elements
}
[13703]=>
array(2) {
[0]=>
string(1) "7"
[1]=>
string(1) "6"
.....
..... more elements
}
[13774]=>
array(1) {
[0]=>
string(1) "7"
.....
..... more elements
}
}
I want to make it like below
array(
'13702'=> 13,//this is the sum of strings inside it
'13703'=> 13,
'13774'=> 7,
);
Please help
Answer
Solution:
No need for array reduce, a simplearray_map
applyingarray_sum
on each sub array should suffice:
$result = array_map('array_sum', $array);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.