【问题标题】:How to count items in multidimensional array, separating them by "type"? [closed]如何计算多维数组中的项目,按“类型”分隔它们? [关闭]
【发布时间】:2021-07-07 07:51:34
【问题描述】:

我有一些项目,我从多维数组中的 db 获取,比如这个(只是数组的一部分),如何用 php 代码通过键“类型”来计算它们,问题是 - 可以不止一个项目键“计数”(如数组的第一部分)?我可以通过“类型”键来计算它们,因为不同的项目有不同的类型,但不知道如何计算它们的多个。示例:我可以得到结果:计数类型 0 = 2,类型 2 = 1,按“类型”计数键,但不知道如何获得正确计数:计数类型 0 = 5,类型 2 = 2 因为我在“count”键中没有 1 项。

                Array
(
    [id] => 212
    [type] => 0
    [number] => 342
    [price] => 10
    [count] => 1
    [price_count] => (€10-1pcs)
)
Array
(
    [id] => 213
    [type] => 0
    [number] => 343
    [price] => 10
    [count] => 4
    [price_count] => (€10-4pcs)
)
Array
(
    [id] => 1014
    [type] => 2
    [number] => 344
    [price] => 10
    [count] => 2
    [price_count] => (€10-2pcs)
)

【问题讨论】:

  • 到目前为止你尝试过什么?你被困在哪里了?为什么不编写一些循环遍历该数组并计数的代码呢?
  • 从答案中尝试代码

标签: php arrays multidimensional-array


【解决方案1】:

你可以创建第二个数组并foreach第一个数组:

$array = [
    [
    'id' => 212,
    'type' => 0,
    'number' => 342,
    'price' => 10,
    'count' => 1,
    'price_count' => '(€10-1pcs)'
]
,[
    'id' => 213,
    'type' => 0,
    'number' => 343,
    'price' => 10,
    'count' => 4,
    'price_count' => '(€10-4pcs)'
]
,[
    'id' => 1014,
    'type' => 2,
    'number' => 344,
    'price' => 10,
    'count' => 2,
    'price_count' => '(€10-2pcs)'
]];

$newArr = ['type' => [], 'count' => []];
foreach($array as $secondArray){
    foreach($secondArray as $key => $value){
        if($key === 'type'){
            if(in_array($value, $newArr['type'])){
                $newArr['type'][$value]++;
            }else{
                $newArr['type'][$value] = 1;  
            }
        }
        if($key === 'count'){
            if(in_array($value, $newArr['count'])){
                $newArr['count'][$value]++;
            }else{
                $newArr['count'][$value] = 1;  
            }
        }
    }
}
print_r($newArr);

这段代码将输出:

Array
(
    [type] => Array
        (
            [0] => 1
            [2] => 1
        )

    [count] => Array
        (
            [1] => 1
            [4] => 1
            [2] => 1
        )

)

您可以扩展我的代码硬编码其他字段,也可以动态创建所有字段

【讨论】:

  • 奇怪,如果我从你的代码中使用数组,得到和你一样的输入,如果使用我的数组 - 得到:Array ( [type] => Array ( ) [count] => Array ( ) ) Array ( [type] => Array ( ) [count] => Array ( ) ) Array ( [type] => Array ( ) [count] => Array ( ) )
  • 我只能看到你在问题中写的内容:) 所以用正确的数组更新
  • 数组 ( [type] => 数组 ( [0] => 1 ) [count] => 数组 ( [1] => 1 [4] => 1 [2] => 1 ) ),对不起,我把代码放错了地方。
  • 所以它是正确的:)?
  • 是的代码是正确的,但不能解决我的问题,我需要得到像 Type 0 count = 5 , type 2 count = 2 , cos type mean item type,我需要计算普通计数分开的结果按类型。
【解决方案2】:
$count = [];

foreach ($arr as $key => $value) {
   if (!isset($count[$value['type']])) {
        $count[$value['type']] = 0;
   }
    
    $count[$value['type']] += $value['count'];
}

print_r($count);

【讨论】:

  • 只按类型计算键的计数,忽略计数键。我需要获取项目数,数组中的 1 和 2 项目是相同的项目(类型 = 0),3 是另一个项目(类型 = 2)。我需要获取项目计数:键入 0 项目计数 = 5,键入 2 项目计数 = 2,计数中的 cos 是项目计数。这是我的问题,我不知道如何计算第二个键“count”
  • 你需要Type = count of this type 吗?
  • 我需要:Type 0(count keys Type 0 * count if count > 1),Type 2(count keys Type 2 * count if count > 1)共同点:这个数组 - 显示数据库中的项目, 项目类型 0 是 5 件。 item type 2 是 2pcs ,无法完成,只获取 item type 0 是 2(cos 两个 key 类型 0) 和 item type 2 是 1(cos 一个 key 类型 2),如何计算相似(相同类型项目)第一个和第二个是类型 0 - 相同的项目
  • @Amimistik 更新答案
  • 获取数组( [0] => 7 ),是否可以将它们分开?要获取类型 0 = 5,请键入 2 = 2?
【解决方案3】:

试试这个来获取01类型的计数

echo array_count_values(array_column($array, 'type'))[0]; // outputs: 2

在哪里

$array 是你的数组

'type' 是键名

[0]是你要统计的key值

【讨论】:

  • 简单的根本不显示
  • 只计算键“类型”,我需要根据类型和计数键来计算,以获得结果类型 0 = 5,类型 2 = 2
  • $type0 = array_count_values(array_column($array, 'type'))[0]; // 输出:2
  • $type2 = array_count_values(array_column($array, 'type'))[2]; // 输出:1
  • 另外,你的问题不是很清楚,需要什么回复?
猜你喜欢
  • 2011-07-08
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
相关资源
最近更新 更多