php中计算二维数组中某一元素之和
程序员文章站
2022-04-08 22:36:25
...
本篇文章介绍的内容是php中计算二维数组中某一元素之和 ,现在在这里分享给大家,有需要的朋友可以参考一下
<meta charset="utf-8"> <?php $ar = array( 2 => array( 'catid' => '6', 'cat' => 'dd', 'catdir' => '1522332388000',//2018-03-29 22:06:28 ), 6=> array( 'catid' => '5', 'cat' => 'dd', 'catdir' => '1522329351000'//2018-03-29 21:15:51 ), 7=> array( 'catid' => '4', 'cat' => 'ddfgd', 'catdir' => '1522328237000'//2018-03-29 20:56:15 ), 9=> array( 'catid' => '3', 'cat' => 'dd', 'catdir' => '1522327772000'//2018-03-29 20:49:32 ), 10=> array( 'catid' => '2', 'cat' => 'dvvd', 'catdir' => '1522325351000'//2018-03-29 20:09:11 ), 5 => array( 'catid' => '1', 'cat' => 'ddfgd', 'catdir' => '1522323606000',//2018-03-29 19:40:06 ), ); echo array_sum(array_map(create_function('$val', 'return $val["catid"];'), $ar)); ?>
原例子:
[0] => array(5) { ["id"] => string(2) "11" ["name"] => string(5) "1.jpg" ["suffix"] => string(3) "jpg" ["url"] => string(29) "./Uploads/1/5292f55d208e8.jpg" ["size"] => string(6) "715895" } [1] => array(5) { ["id"] => string(2) "12" ["name"] => string(22) "minisite数据库.docx" ["suffix"] => string(4) "docx" ["url"] => string(30) "./Uploads/1/5292f5606a52c.docx" ["size"] => string(5) "16667" }
我要计算size的和
(1)
echo
array_sum
(
array_map
(create_function(
'$val'
,
'return $val["size"];'
),
$arr
));
或者
echo
array_sum
(
array_map
(
function
(
$val
){
return
$val
[
'size'
];},
$arr
));
(2)
$sum
= 0;
foreach
(
$arr
as
$item
){
$sum
+= (int)
$item
[
'size'
];
}
引用:https://www.cnblogs.com/isykw/p/6217097.html
相关推荐:
以上就是php中计算二维数组中某一元素之和 的详细内容,更多请关注其它相关文章!
上一篇: js indexOf 函数用法实例