将二维数组的行按一列分组,并对每组内的另一列求和

发布于 2024-12-04 13:59:05 字数 538 浏览 1 评论 0原文

我已经四处寻找,但我已经束手无策,所以我会让这件事变得简单。这就是我在 print_r 中所拥有的:

[
    ['name' => 'client interaction', 'y' => 2.7],
    ['name' => 'client interaction', 'y' => 0.1],
    ['name' => 'project planning', 'y' => 0.1],
    ['name' => 'client interaction', 'y' => 5.9],
]

这就是我想要的:

[
    ['name' => 'client interaction', 'y' => 8.7],
    ['name' => 'project planning', 'y' => 0.1]
]

I have searched around and I am at my wits end so I will make this easy. This is what I have in a print_r:

[
    ['name' => 'client interaction', 'y' => 2.7],
    ['name' => 'client interaction', 'y' => 0.1],
    ['name' => 'project planning', 'y' => 0.1],
    ['name' => 'client interaction', 'y' => 5.9],
]

And this is what I want it to be:

[
    ['name' => 'client interaction', 'y' => 8.7],
    ['name' => 'project planning', 'y' => 0.1]
]

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

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

发布评论

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

评论(3

我是男神闪亮亮 2024-12-11 13:59:05

您想要的数组是否绝对有必要使用数字索引?如果我要这样做,我会这样做(尽管与您想要的数组不同)

$newArray = array();
foreach($array as $item)
{
  if(!isset($newArray[$item["name"]]))
    $newArray[$item["name"]] = 0;

  $newArray[$item["name"]] += $item["y"];
}

这将为您提供一个具有以下结构的数组:

Array
(
  [client interaction] => 8.7
  [project planning] => 0.1
)

要取回键,您只需使用 foreach< 的第二种形式/code>循环

foreach($newArray as $name => $y)

$name将包含原始数组中的name$yy

Is it absolutely necessary that your desired array use numeric indeces? If I were to do this I would do it this way (not the same as your desired array though)

$newArray = array();
foreach($array as $item)
{
  if(!isset($newArray[$item["name"]]))
    $newArray[$item["name"]] = 0;

  $newArray[$item["name"]] += $item["y"];
}

This will give you an array of this structure:

Array
(
  [client interaction] => 8.7
  [project planning] => 0.1
)

To get the keys back you simply use the second form of the foreach loop

foreach($newArray as $name => $y)

$name will contain the name and $y the y in your original array.

黯淡〆 2024-12-11 13:59:05
$hash = array();
foreach ($sourceArray as $key=>$value) {
   $y = 0;
   if (isset($hash{$value{'name'}})) {
      $y = $hash{$value{'name'}};
   }

   $hash{$value{'name'}} = $y + $value{'y'};
}

$result = array();
foreach ($hash as $key => $value) {
  $result[] = array( 'name' => $key, 'value' => $value );
}
print_r($result);

最后一个循环只是将 $hash 转换为您指定的确切格式。


说明:

$hash

是一个临时结构,用于计算每个唯一名称的总和。

foreach ($sourceArray as $key=>$value) {

这将遍历您的原始数组 ($sourceArray) 并提取每个元素。

   $y = 0;

初始化一个变量来存储属于该名称的当前总和。

   if (isset($hash{$value{'name'}})) {

检查给定名称的值是否已存储在 $hash 中。

      $y = $hash{$value{'name'}};
   }

将 $y 设置为先前为给定名称计算的总和(如果有的话)。

   $hash{$value{'name'}} = $y + $value{'y'};
}

将给定名称的总和存储到我们的临时结构$hash中。

 $result = array();
    foreach ($hash as $key => $value) {
      $result[] = array( 'name' => $key, 'value' => $value );
    }

将 $hash 转换为您想要的格式。

$result[] = ... 中的 empy [] 将表达式的右侧添加到 $result 数组的末尾。

$hash = array();
foreach ($sourceArray as $key=>$value) {
   $y = 0;
   if (isset($hash{$value{'name'}})) {
      $y = $hash{$value{'name'}};
   }

   $hash{$value{'name'}} = $y + $value{'y'};
}

$result = array();
foreach ($hash as $key => $value) {
  $result[] = array( 'name' => $key, 'value' => $value );
}
print_r($result);

The last loop is just to get $hash into the exact format you specified.


Explanation:

$hash

Is a temporary structure used to compute the sums for each unique name.

foreach ($sourceArray as $key=>$value) {

This goes through your original array ($sourceArray) and pulls out each element.

   $y = 0;

Initializes a variable to store the current sum that belongs with that name.

   if (isset($hash{$value{'name'}})) {

Checks to see if a value has already been stored in $hash for the given name.

      $y = $hash{$value{'name'}};
   }

Sets $y to the previously calculated sum for the given name, if there was one.

   $hash{$value{'name'}} = $y + $value{'y'};
}

Stores the sum for the given name into our temporary structure $hash.

 $result = array();
    foreach ($hash as $key => $value) {
      $result[] = array( 'name' => $key, 'value' => $value );
    }

converts $hash to the format you desired.

The empy []'s in $result[] = ... add the right hand side of the expression to the end of the $result array.

聚集的泪 2024-12-11 13:59:05
$mixed = array(); // Should contain your array shown above
$groups = array();
$newArray = array(); // Will contain your new array
$counter = 0;

foreach( $mixed as $mix )
{
    if ( isset($groups[$mix['name']]) )
    {
        $newArray[$groups[$mix['name']]]['y'] += $mix['y'];
    }
    else
    {
        $groups[$mix['name']] = $counter;
        $newArray[] = $mix;
        $counter++;
    }
}

http://codepad.org/EjdHxgvt

$mixed = array(); // Should contain your array shown above
$groups = array();
$newArray = array(); // Will contain your new array
$counter = 0;

foreach( $mixed as $mix )
{
    if ( isset($groups[$mix['name']]) )
    {
        $newArray[$groups[$mix['name']]]['y'] += $mix['y'];
    }
    else
    {
        $groups[$mix['name']] = $counter;
        $newArray[] = $mix;
        $counter++;
    }
}

http://codepad.org/EjdHxgvt

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