在这个数组输出中,我如何计算类型的数量?或者更好的是,如何在计算时排除additional_data?
Array (
[124] =>
Array (
[type] => 0
[value] => 4 Pack
[label] => 4 Pack
)
[125] =>
Array (
[type] => 0
[value] => 6 Pack
[label] => 6 Pack
)
[126] =>
Array (
[type] => 0
[value] => 12 Pack
[label] => 12 Pack
)
[additional_data] => {"swatch_input_type":"text","update_product_preview_image":"1","use_product_image_for_swatch":0} )
尝试过
count(array_column($swatchLists, 'type'));
但输出为0
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
请尝试以下代码
$countResult = array(); foreach( $swatchLists as $item ){ if( isset($item['type']) == false ){ continue; // 排除不包含'type'的数组 } $type = $item['type']; if( isset($countResult[$type]) ){ $countResult[$type]++; }else{ $countResult[$type] = 1; } } var_dump($countResult);每种类型的计数都将存储在
$countResult中,数组键是类型值,数组值是计数。