Php 获取多维数组中的元素计数并指定筛选器
我想获得多维数组中Php 获取多维数组中的元素计数并指定筛选器,php,arrays,multidimensional-array,count,Php,Arrays,Multidimensional Array,Count,我想获得多维数组中国家记录的计数。 我也希望得到相同的计数,但通过记录过滤,因此荷兰的计数 我试过这个: $total = array_sum(array_map("count", $data)); echo "total: " . $total; 它统计数组中的记录数 我试过: $element = 'institutionID'; $count = array_count_values(array_column($data, 'institution
国家
记录的计数。
我也希望得到相同的计数,但通过记录过滤,因此荷兰的计数
我试过这个:
$total = array_sum(array_map("count", $data));
echo "total: " . $total;
它统计数组中的记录数
我试过:
$element = 'institutionID';
$count = array_count_values(array_column($data, 'institutionID'))[$element];
echo $count;
但是什么也没有归还。我该怎么做
数据示例
Array
(
[0] => Array
(
[higherGeographyID] => http://vocab.getty.edu/tgn/7016845
[higherGeography] => None
[continent] => Europe
[waterBody] => None
[islandGroup] => None
[island] => None
[country] => The Netherlands
[countryCode] => NL
[stateProvince] => Groningen
[county] => Groningen
[municipality] => Groningen
[locality] => Groningen
[verbatimLocality] => None
[minimumElevationInMeters] => None
[maximumElevationInMeters] => None
[verbatimElevation] => None
[minimumDepthInMeters] => None
[maximumDepthInMeters] => None
[verbatimDepth] => None
[minimumDistanceAboveSurfaceInMeters] => None
[maximumDistanceAboveSurfaceInMeters] => None
[locationAccordingTo] => None
[locationRemarks] => None
[decimalLatitude] => None
[decimalLongitude] => None
[geodeticDatum] => None
[coordinateUncertaintyInMeters] => None
[coordinatePrecision] => None
[pointRadiusSpatialFit] => None
[verbatimCoordinates] => None
[verbatimLatitude] => None
[verbatimLongitude] => None
[verbatimCoordinateSystem] => None
[verbatimSRS] => None
[footprintWKT] => None
[footprintSRS] => None
[footprintSpatialFit] => None
[georeferencedBy] => None
[georeferencedDate] => None
[georeferenceProtocol] => None
[georeferenceSources] => None
[georeferenceVerificationStatus] => None
[georeferenceRemarks] => None
)
[1] => Array
(
...
国家/地区
记录计数:
$dataCountry = array_column($data, 'country');
echo "total: ", count($dataCountry);
荷兰的计数
:
$dataCountryCount = array_count_values(array_filter($dataCountry));
echo $dataCountryCount['The Netherlands'] ?? 0;
多搜索筛选器:
$filter = ['country' => 'The Netherlands', 'continent' => 'Europe'];
$dataFilter = array_filter($data, function($item) use ($filter) {
return $filter == array_intersect_key($item, $filter);
});
echo 'total:', count($dataFilter);
print_r($dataFilter);
您可以在下面尝试:
$data = json_decode($data_json,true);
echo count_filtered_by($data);
// Returns (int) 3
echo count_filtered_by($data,'country');
// Returns (int) 3
echo count_filtered_by($data,'country','The Netherlands');
// Retruns (int) 2
echo count_filtered_by($data,'country','UK');
// Returns (int) 1
echo count_filtered_by($data,'country','uK',true);
// Returns (int) 0
/**
* Count filtered by
*
* @param array $data
* @param string|nullable $key
* @param string|nullable $value
* @param bool $strict Default is (Case Insensitive)
*
* @return int count filtered by
*/
function count_filtered_by($data,$key = NULL,$value = NULL,$strict = false) {
$column = array_column($data,$key);
if($key === NULL) return count($data);
if($value === NULL ) return count($column);
// Remove empty values
$filtered = array_filter($column);
$count = 0;
foreach($filtered as $column) {
if(!$strict) {
$column = strtolower($column);
$value = strtolower($value);
}
if($column === $value) $count++;
}
return $count;
}
所以我在这里测试你的数据
$data_json = '[
{
"higherGeographyID": "http:\/\/vocab.getty.edu\/tgn\/7016845",
"higherGeography": "None",
"continent": "Europe",
"waterBody": "None",
"islandGroup": "None",
"island": "None",
"country": "The Netherlands",
"countryCode": "NL",
"stateProvince": "Groningen",
"county": "Groningen",
"municipality": "Groningen",
"locality": "Groningen",
"verbatimLocality": "None",
"minimumElevationInMeters": "None",
"maximumElevationInMeters": "None",
"verbatimElevation": "None",
"minimumDepthInMeters": "None",
"maximumDepthInMeters": "None",
"verbatimDepth": "None",
"minimumDistanceAboveSurfaceInMeters": "None",
"maximumDistanceAboveSurfaceInMeters": "None",
"locationAccordingTo": "None",
"locationRemarks": "None",
"decimalLatitude": "None",
"decimalLongitude": "None",
"geodeticDatum": "None",
"coordinateUncertaintyInMeters": "None",
"coordinatePrecision": "None",
"pointRadiusSpatialFit": "None",
"verbatimCoordinates": "None",
"verbatimLatitude": "None",
"verbatimLongitude": "None",
"verbatimCoordinateSystem": "None",
"verbatimSRS": "None",
"footprintWKT": "None",
"footprintSRS": "None",
"footprintSpatialFit": "None",
"georeferencedBy": "None",
"georeferencedDate": "None",
"georeferenceProtocol": "None",
"georeferenceSources": "None",
"georeferenceVerificationStatus": "None"
},
{
"higherGeographyID": "http:\/\/vocab.getty.edu\/tgn\/7016845",
"higherGeography": "None",
"continent": "Europe",
"waterBody": "None",
"islandGroup": "None",
"island": "None",
"country": "The Netherlands",
"countryCode": "NL",
"stateProvince": "Groningen",
"county": "Groningen",
"municipality": "Groningen",
"locality": "Groningen",
"verbatimLocality": "None",
"minimumElevationInMeters": "None",
"maximumElevationInMeters": "None",
"verbatimElevation": "None",
"minimumDepthInMeters": "None",
"maximumDepthInMeters": "None",
"verbatimDepth": "None",
"minimumDistanceAboveSurfaceInMeters": "None",
"maximumDistanceAboveSurfaceInMeters": "None",
"locationAccordingTo": "None",
"locationRemarks": "None",
"decimalLatitude": "None",
"decimalLongitude": "None",
"geodeticDatum": "None",
"coordinateUncertaintyInMeters": "None",
"coordinatePrecision": "None",
"pointRadiusSpatialFit": "None",
"verbatimCoordinates": "None",
"verbatimLatitude": "None",
"verbatimLongitude": "None",
"verbatimCoordinateSystem": "None",
"verbatimSRS": "None",
"footprintWKT": "None",
"footprintSRS": "None",
"footprintSpatialFit": "None",
"georeferencedBy": "None",
"georeferencedDate": "None",
"georeferenceProtocol": "None",
"georeferenceSources": "None",
"georeferenceVerificationStatus": "None"
},
{
"higherGeographyID": "http:\/\/vocab.getty.edu\/tgn\/7016845",
"higherGeography": "None",
"continent": "Europe",
"waterBody": "None",
"islandGroup": "None",
"island": "None",
"country": "UK",
"countryCode": "UK",
"stateProvince": "Groningen",
"county": "Groningen",
"municipality": "Groningen",
"locality": "Groningen",
"verbatimLocality": "None",
"minimumElevationInMeters": "None",
"maximumElevationInMeters": "None",
"verbatimElevation": "None",
"minimumDepthInMeters": "None",
"maximumDepthInMeters": "None",
"verbatimDepth": "None",
"minimumDistanceAboveSurfaceInMeters": "None",
"maximumDistanceAboveSurfaceInMeters": "None",
"locationAccordingTo": "None",
"locationRemarks": "None",
"decimalLatitude": "None",
"decimalLongitude": "None",
"geodeticDatum": "None",
"coordinateUncertaintyInMeters": "None",
"coordinatePrecision": "None",
"pointRadiusSpatialFit": "None",
"verbatimCoordinates": "None",
"verbatimLatitude": "None",
"verbatimLongitude": "None",
"verbatimCoordinateSystem": "None",
"verbatimSRS": "None",
"footprintWKT": "None",
"footprintSRS": "None",
"footprintSpatialFit": "None",
"georeferencedBy": "None",
"georeferencedDate": "None",
"georeferenceProtocol": "None",
"georeferenceSources": "None",
"georeferenceVerificationStatus": "None"
}
]';
如果我理解正确,您可以这样做:
//Example data for testing
$arr = [
['a'=> ['country' => 'Netherland']],
['e'=> ['country' => 'Netherland']],
['b'=> ['country' => 'Sweden']],
['c'=> ['country' => 'Netherland'], 'a' => ['country' => 'Netherland']],
['f'=> ['country' => 'Sweden']],
];
//Init som global variables
$count_countries = 0; //Number of occurences of 'country'
$check_country = 'Netherland'; //What country to check
$count_country = 0; //Number of occurences for chosen country to check
//Walk through array recursively and count each found occurence of
//country and the actual country to check
array_walk_recursive($arr, function($item, $key) {
global $count_countries, $count_country, $check_country;
if ($key == 'country') $count_countries++;
if ($item == $check_country) $count_country++;
});
//Output result
echo 'count for ' . $check_country . ' = ' . $count_country . '<br>';
echo 'count for countries = ' . $count_countries;
您想计算国家/地区
键的出现次数,无论其深度有多深?如果您可以修改“数据示例”以包含一些空白层次结构,将非常有用,indentation@GetSet是的,不管深度有多深,但它始终处于此级别。@添加了GetSet空格以使相同的数据更易于阅读。确定。国家如何划分?也就是说,对于国家
,您有什么“数据结构”或“格式”?该字段中是否可以有多个值?这似乎是一个填充问题,但我想知道,当数组键存在时,为什么不循环并增加一个cnt
var?只有当“国家”是数组的第一级时,这才算正确,我不认为这是OP所期望的。@bestprogrammerintheworld,但它似乎有效。。。我会做一些手工计算来检查它的行为。。。有没有办法添加第二个过滤器,比如大陆['Europe']
?从python(django)回来后,我仍在学习php。@SpatialDigger,添加了一个多搜索过滤器的示例。@SpatialDigger-是的,它可以根据您的示例工作,但如果您在数组中再降低一个级别(使用国家/地区),它将不会工作。
count for Netherland = 4
count for countries = 6