You can try this below:
$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;
}
So I test your data here
$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"
}
]';
countrykey, no matter how deep?countrydelimited? That is, what "data structure" or "format" do you have forcountry? Can it be more than one value in that field? It might seem like a filler question but am wondering why you just don't loop through and increment acntvar when that array key exists?