Home > Backend Development > PHP Tutorial > Calculate the sum of an element in a two-dimensional array in php

Calculate the sum of an element in a two-dimensional array in php

不言
Release: 2023-03-23 09:12:01
Original
2822 people have browsed it

The content of this article is to calculate the sum of a certain element in a two-dimensional array in PHP. Now I will share it with you. Friends in need can refer to it


 <meta charset="utf-8">

<?php
$ar = array(
    2 => array(
        &#39;catid&#39; => &#39;6&#39;,
		&#39;cat&#39; => &#39;dd&#39;,
        &#39;catdir&#39; => &#39;1522332388000&#39;,//2018-03-29 22:06:28
    ),
    6=> array(
        &#39;catid&#39; => &#39;5&#39;,
		&#39;cat&#39; => &#39;dd&#39;,
        &#39;catdir&#39; => &#39;1522329351000&#39;//2018-03-29 21:15:51
    ),
    7=> array(
        &#39;catid&#39; => &#39;4&#39;,
		&#39;cat&#39; => &#39;ddfgd&#39;,
        &#39;catdir&#39; => &#39;1522328237000&#39;//2018-03-29 20:56:15
    ),
    9=> array(
        &#39;catid&#39; => &#39;3&#39;,
		&#39;cat&#39; => &#39;dd&#39;,
        &#39;catdir&#39; => &#39;1522327772000&#39;//2018-03-29 20:49:32
    ),
    10=> array(
        &#39;catid&#39; => &#39;2&#39;,
		&#39;cat&#39; => &#39;dvvd&#39;,
        &#39;catdir&#39; => &#39;1522325351000&#39;//2018-03-29 20:09:11
    ),
    5 => array(
        &#39;catid&#39; => &#39;1&#39;,
		&#39;cat&#39; => &#39;ddfgd&#39;,
        &#39;catdir&#39; => &#39;1522323606000&#39;,//2018-03-29 19:40:06
    ),

);

echo array_sum(array_map(create_function(&#39;$val&#39;, &#39;return $val["catid"];&#39;), $ar));

?>
Copy after login


Original example:


[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"
    }
Copy after login

I want to calculate the sum of sizes

(1)

echo array_sum(array_map(create_function('$val', 'return $val["size"];'), $arr));

or

echo array_sum(array_map(function($val){return $val ['size'];}, $arr));

(2)


$sum = 0;

foreach($arr as $item){

$sum += (int) $item['size'];

}


Quote: https://www.cnblogs.com/isykw/p/6217097.html

Related recommendations:

How to convert PHP two-dimensional array into one-dimensional array

How to remove duplicates from PHP two-dimensional array

The above is the detailed content of Calculate the sum of an element in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template