在数组的每个列中,列的总和数组值3级

发布于 2025-01-27 12:24:25 字数 758 浏览 5 评论 0原文

我正在尝试在多维数组列内的列上使用array_sum()

例如:我有一个看起来像这样的数组:

$array = [
    [['value' => 1100], ['value' => 1000], ['value' => 3000]],
    [['value' => 1200], ['value' => 2000], ['value' => 2000]],
    [['value' => 1300], ['value' => 4000], ['value' => 1000]],
];

我尝试了:

$arr = [];
foreach($array as $point){
   $arr[] = array_sum(array_column($array, $point[0]['value']));
}
print_r($arr);

但是我期望此输出:

[['value'=> 3600],['value'=> 7000],['value'=> 6000]]

或更简单:[3600,7000,6000]

在此处测试: https://onecompiler.com/php/3y3mxqky9

I'm trying to use array_sum() on columns within columns of a multidimensional array.

For eg: I have an array that looks like this:

$array = [
    [['value' => 1100], ['value' => 1000], ['value' => 3000]],
    [['value' => 1200], ['value' => 2000], ['value' => 2000]],
    [['value' => 1300], ['value' => 4000], ['value' => 1000]],
];

I tried with:

$arr = [];
foreach($array as $point){
   $arr[] = array_sum(array_column($array, $point[0]['value']));
}
print_r($arr);

but I'm expecting this output:

[['value' => 3600], ['value' => 7000], ['value' => 6000]]

Or more simply: [3600, 7000, 6000]

Tested here: https://onecompiler.com/php/3y3mxqky9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2025-02-03 12:24:25

您可以按这样的列来概括您的专栏:

foreach($array as $key => $point){
  $arr[] = array_sum(array_column( array_column($array,$key),'value'));
}
print_r($arr);

You can sum your columns doing like this:

foreach($array as $key => $point){
  $arr[] = array_sum(array_column( array_column($array,$key),'value'));
}
print_r($arr);
自找没趣 2025-02-03 12:24:25

由于您希望获得垂直和水平的总和,因此您使用的Array_column样式无法正常工作。您只需使用以下两个嵌套循环即可实现此目标:

<?php

$arr = [];
foreach($array as $point){
   foreach($point as $k => $v){
      $arr[$k] = ($arr[$k] ?? 0) + $v['value'];
   }
}
print_r($arr); 

在线demo

Since you wish to have the sum vertically and not horizontally, the array_column style you used won't work. You can simply achieve this with 2 nested loops like below:

<?php

$arr = [];
foreach($array as $point){
   foreach($point as $k => $v){
      $arr[$k] = ($arr[$k] ?? 0) + $v['value'];
   }
}
print_r($arr); 

Online Demo

野鹿林 2025-02-03 12:24:25

转置输入数组,然后隔离并总和value数据列。

$ array to array_map()的variadic发行是换零件。

换句话说,array_map(fn(... $ col)=&gt; $ col,... $ array)将数据行转换为数据列。

代码:( demo

var_export(
    array_map(
        fn(...$col) => array_sum(array_column($col, 'value')),
        ...$array
    )
);

输出:

array (
  0 => 3600,
  1 => 7000,
  2 => 6000,
)

Transpose your input array, then isolate and sum the value column of data.

The variadic offering of $array to array_map() is the transposing part.

In other words, array_map(fn(...$col) => $col, ...$array) converts rows of data into columns of data.

Code: (Demo)

var_export(
    array_map(
        fn(...$col) => array_sum(array_column($col, 'value')),
        ...$array
    )
);

Output:

array (
  0 => 3600,
  1 => 7000,
  2 => 6000,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文