本篇文章介紹的內容是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)
= 0;
(
$arr
as
$item
#){
$sum
+= (int)
$item
[
'size'
##];
}
#引用:https://www.cnblogs.com/isykw/p/6217097.html
#相關推薦:
#以上是php中計算二維數組中某一元素之和的詳細內容。更多資訊請關注PHP中文網其他相關文章!