English 中文(简体)
具有相同关键意义的多阵列的运行价值
原标题:Operation value of multiple array that have the same key
  • 时间:2011-03-17 08:11:44
  •  标签:
  • php
  • arrays

页: 1 拥有一系列阵容

array(4) {
      [0]=>
      array(3) {
        ["a"]=>float(1000)
        ["b"]=>float(3)
        ["c"]=>float(500)
      }
      [1]=>
      array(3) {
        ["a"]=>float(1000)
        ["b"]=>float(852)
        ["c"]=>float(500)
      }
      [2]=>
      array(3) {
        ["a"]=>float(1000)
        ["b"]=>float(5)
        ["c"]=>float(500)
      }
      [3]=>
      array(1) {
        ["e"]=>float(1000)
      }
    }

其结果将总结同样关键因素的全部价值,即:

$result = 
  array(
      "a" =>3000,
      "b"=>900,
      "c"=>1500,
      "e"=>1000
  )

Anybody could help me todo this.

thanks.

最佳回答

Pseudo:

result <- new array                       # array holding result
foreach entry1 in array:                  # iterate outer array
    foreach entry2 in entry1:             # iterate each inner array
        if not exists result[entry2.key]: # if key is not already in result...
            result[entry2.key] = 0        # ... add key and set value to zero
        result[entry2.key] += value       # increment result for key with value from inner array

http://www.un.org/Depts/DGACM/index_chinese.htm

问题回答

The trick for this is ofcourse to do some sort of iteration over your data, using those string-keys as identifiers. One way of approaching it would be using 2 nested foreaches ( one over the container, one over the individual keys and collecting the data in a central array:

$results = array();
foreach ($array as $elements)
{
        foreach ($elements as $key => $value)
        {   
                if (!isset($results[$key]))
                        $results[$key] = 0;

                $results[$key] += $value;
        }   
}

一种不同的方式是使植物检疫措施对你有利:

$results = array();

array_walk_recursive(
        $array,
        function($value, $key) use (&$results) {
                if (!isset($results[$key]))
                        $results[$key] = 0;

                $results[$key] += $value;
        }
);

This little function will do the job for you.

function SummarizeFosArray($array) {
    $results=array();
    foreach ($array as $a) {
      foreach ($a as $k=>$v) {
        $results[$k]+=$v;
      }
    }
    return $results;
}

页: 1

$array = array(
        array( a  => 1000,  b  =>3,  c => 500),
        array( a  => 1000,  b  =>852,  c => 500),
        array( a  => 1000,  b  =>5,  c => 500),
        array( e  => 1000)
        );

$result = array();  

foreach($array as $arr)
{
    foreach($arr as $a => $val){
        $result[$a] += $val;
    }
}

echo "<pre>";
print_r($result);
echo "</pre>";  

阁下

Array
(
    [a] => 3000
    [b] => 860
    [c] => 1500
    [e] => 1000
)

rel=“nofollow”>array_walk_recursive( 职能。 详细情况调查手册。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签