Skip to content

Instantly share code, notes, and snippets.

@JBlond
Last active January 7, 2025 11:50
Show Gist options
  • Save JBlond/ea0ea99b2efb9e29aa10a9154950c6f4 to your computer and use it in GitHub Desktop.
Save JBlond/ea0ea99b2efb9e29aa10a9154950c6f4 to your computer and use it in GitHub Desktop.
search
<?php
protected function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, $this->search($subarray, $key, $value));
}
return $results;
}
protected function getCountToValue($key, $value, array $array){
return array((string) $value => count($this->search($array, $key, $value)));
}
protected function getAggregations()
{
$return = array();
$result = $this->engine->getResult();
$products = array();
$product_counter = 0;
foreach ($result['hits']['hits'] as $unique){
//print_r($unique);
$products["$product_counter"] = $unique['_source'];
$product_counter++;
}
$hits = $result['hits']['hits'];
foreach ($this->engine->filter_able as $filter_key => $filter_value){
foreach ($filter_value as $filter => $filter_single_value){
$unique = array_unique(array_column($products, $filter_single_value));
foreach ($unique as $item){
$return['aggregation']["$filter"][] = $this->getCountToValue($filter_single_value, $item, $hits);
}
}
}
print_r($return);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment