I have a two dimensional array.
arr["id1"][10]
arr["id2"][20]
How to i add the second value of each item together.
eg 10 + 20 = 30 (return)
I need to run through all items in the array and add the second value together.
$res = 0;
foreach($arr as $v) {
$res += $v;
}
like this you add the value of each item to $res
This is sooo weird, but I'm in the mood for some puzzle solving.
<?php
$odd_data["id1"][10] = 'Food item one';
$odd_data["id2"][20] = 'Food item two';
echo add_the_second_array_keys_of_the_array($odd_data);
function add_the_second_array_keys_of_the_array($odd_data)
{
$total = 0;
foreach($odd_data as $id)
{
foreach($id as $number=>$the_message)
{
$total += $number;
}
}
return $total;
}
function collapse($array){
return array_keys($array)[0];
}
print array_sum(array_map('collapse', $arr));
$result = array();
function sort($arr) {
for($i=0; $i < sizeof($arr); $i+2)
{
$result[$i] = $arr[$i] + $arr[$i+1];
}
}
this will have result storing the summing values. (the keys's although will jump by 2 eg 1, 3, 5 ...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.