English 中文(简体)
一组2个阵列的组增长数据,一栏,一栏汇总每个组别的另一栏,以建立一个统一、联系阵列。
原标题:Group row data of a 2d array by one column and sum another column within each group to create a flat, associative array

我有问题。 我想总结一下特定数量(qty)。

我想得到:

blyszczaca Normal = 6;

czapkoszal Normal = 6;

czapkoszal Luxury = 2;

etola Wolf = 1;

etola Normal = 2;

本阵列:

Array
(
    [0] => Array
        (
            [produkt] => blyszczaca
            [model] => Normal
            [price] => 27.00
            [qty] => 2
        )

    [1] => Array
        (
            [0] => blyszczaca
            [1] => Normal
            [2] => 27.00
            [3] => 2
        )

    [2] => Array
        (
            [0] => blyszczaca
            [1] => Normal
            [2] => 27.00
            [3] => 2
        )

    [3] => Array
        (
            [0] => czapkoszal
            [1] => Normal
            [2] => 41.00
            [3] => 2
        )

    [4] => Array
        (
            [0] => czapkoszal
            [1] => Luxury
            [2] => 45.00
            [3] => 2
        )


    [5] => Array
        (
            [0] => czapkoszal
            [1] => Normal
            [2] => 41.00
            [3] => 2
        )

    [6] => Array
        (
            [0] => czapkoszal
            [1] => Normal
            [2] => 41.00
            [3] => 1
        )

    [7] => Array
        (
            [0] => czapkoszal
            [1] => Normal
            [2] => 41.00
            [3] => 1
        )

    [8] => Array
        (
            [0] => etola
            [1] => Wolf
            [2] => 47.00
            [3] => 1
        )

    [9] => Array
        (
            [0] => etola
            [1] => Normal
            [2] => 39.00
            [3] => 1
        )

    [10] => Array
        (
            [0] => etola
            [1] => Normal
            [2] => 39.00
            [3] => 1
        )

)
最佳回答

你们应当创建具有独特钥匙和数量的新阵列,例如:

$sums = [];
foreach ($your_array as $item) {
    // create a unique key as concatenation of `product` and `model`
    $key = $item[ product ] .  :  . $item[ model ];

    // check if such key exists, if not - init key with `0`
    if (!isset($sums[$key])) {
        $sums[$key] = 0;
    }

    // add current `qty` to the value of `$sums[$key]`
    $sums[$key] += $item[ qty ]
}
问题回答

您可在产出阵列中根据<代码>produkt和<代码>model创建一种习俗钥匙。 然后,将<代码>qty添加到该钥匙上。 如下:

// Create a array to store the sum
$sums = array();

// Loop over the input array 
foreach ($input as $value) {

    // create a custom key based on produkt and model
    $key = $value[ produkt ] .     . $value[ model ];

    // initalize the key 
    $sums[$key] = $sums[$key] ?? 0;

    // add the quantity
    $sums[$key] += (int)$value[ qty ];
}




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

热门标签