English 中文(简体)
仅添加配对阵容元素的数值
原标题:Add values of only matching array elements PHP
  • 时间:2012-04-25 19:10:14
  •  标签:
  • php
  • arrays

让我说一下:

Array
(
    [0] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 36
            [linearFt] => 3
        )

    [1] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 36
            [linearFt] => 3
        )
    [2] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 48
            [linearFt] => 5
        )

    [3] => Array
        (
            [filmType] =>  Symphony 25%
            [rollSize] => 36
            [linearFt] => 7
        )
    [4] => Array
        (
            [filmType] =>  Symphony 35%
            [rollSize] => 36
            [linearFt] => 10
        )
    [5] => Array
        (
            [filmType] =>  Symphony 35%
            [rollSize] => 36
            [linearFt] => 10
        )

如何增加奥文特独一无二的阵列要素。 独一无二的我指的是[过滤型]和[滚动式]。

我尝试了多种方法,包括搜索阵列、阵列过滤器和护卫......。 我是PHP的新鲜事,这是NOT的家庭工作。 感谢任何帮助!

因此,我的产出是:

$totalsArray = Array(

    [0] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 36
            [linearFt] => 8
        )

    [1] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 48
            [linearFt] => 3
        )
    [2] => Array
        (
            [filmType] =>  Symphony 25%
            [rollSize] => 48
            [linearFt] => 7
        )
    [3] => Array
        (
            [filmType] =>  Symphony 35%
            [rollSize] => 48
            [linearFt] => 20
        )
最佳回答

Ah. 我将用foreach loop来攻击这一问题,以便每个阵列,然后积累不同类型和规模,并在我来时添加。 它希望:

$filmTotals = array();
$filmTemp   = array();
foreach ($filmInventory as $film){
    $filmTemp[$film[ filmType ]][$film[ rollSize ]] = $filmTemp[$film[ filmType ]][$film[ rollSize ]] + $film[ linearFt ];
}

// A second foreach to resort into the final format, nested to get the keys out to rename them correctly
foreach ($filmTemp as $filmType=>$film){
    foreach ($film as $filmSize=>$filmLength){
        $filmTotals[] = array( filmType =>$filmType,  rollSize =>$filmSize,  linearFt =>$filmLength);
    } //END inner foreach
} // END outer foreach
问题回答

引证:

foreach ($myArray as $value) {
   $sumsArray[$value[ filmType ]_$value[ rollSize ]][ length_tally ][] = $value[ linearFt ];
}

rel=“nofollow”array_sum()和length_tally阵列,以便全面延长。

产出是你所期待的,但应该让你走上正确的道路。





相关问题
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 ...

热门标签