0

I have some code that I have tried modifying so that the array is sorted alphabetically, I thought the following:

public function getOptions(array $aggs, $filterEmptyOptions = true)

Should be:

public function getOptions(array sort($aggs), $filterEmptyOptions = true)

But it doesn't work, where should I add sort() to the array?

EDIT: I am trying to sort the brewery names alphabetically

This is my full code if it makes it easier?

class BreweryBoolMustFilter implements BoolFilterInterface
{

/**
 * @param Query $query
 * @param Query\BoolQuery $bool
 * @param Request $request
 * @param array $filters
 */
public function __construct(Query $query, Query\BoolQuery $bool, Request $request, array &$filters)
{
    if ($request->query->has('brewery') && (int) $request->query->get('brewery'))
    {
        $filters['brewery'] = (int) $request->query->get('brewery');

        $bool->addMust(new Query\Term(['breweryId' => $filters['brewery']]));
    }

    $aggs3 = new Terms('breweries');
    $aggs3->setField('breweryId');
    $aggs3->setSize(100);

    $query->addAggregation($aggs3);
}


/**
 * @param array $aggs
 * @param bool $filterEmptyOptions
 *
 * @return array
 */
public function getOptions(array $aggs, $filterEmptyOptions = true)
{
    $breweriesObjects = (new BreweryQuery)->find();

    $breweries = [];

    foreach ($breweriesObjects as $breweryObject)
    {
        $breweries[$breweryObject->getId()] = [
            'name'      => $breweryObject->getName(),
            'doc_count' => 0,
        ];
    }

    foreach ($aggs['breweries']['buckets'] as $bucket)
    {
        if (isset($breweries[$bucket['key']]))
        {
            $breweries[$bucket['key']]['doc_count'] = $bucket['doc_count'];
        }
    }

    if (true === $filterEmptyOptions)
    {
        foreach ($breweries as $key => $brewery)
        {
            if (0 == $brewery['doc_count'])
            {
                unset($breweries[$key]);
            }
        }
    }

    return $breweries;
}
Will Wright
  • 213
  • 1
  • 13
  • You don't put function calls in the argument specification of a function, you put it in the function body. – Barmar May 25 '17 at 10:35
  • See the duplicate question for lots of information about sorting arrays. Then use that to sort `$breweries` by the `name` values in each element. – Barmar May 25 '17 at 11:25
  • @Barmar Is not a duplicate because the main problem of the question is not only sort, but running functions inside arguments of another, I suggest you to reconsider – capcj May 25 '17 at 11:51

2 Answers2

0

You need to call sort() in the function, not in the parameter list. You should be sorting $breweries after you fill it in, sorting it by the name values. You can use usort() for this.

public function getOptions(array $aggs, $filterEmptyOptions = true)
{
    $breweriesObjects = (new BreweryQuery)->find();

    $breweries = [];

    foreach ($breweriesObjects as $breweryObject)
    {
        $breweries[$breweryObject->getId()] = [
            'name'      => $breweryObject->getName(),
            'doc_count' => 0,
        ];
    }

    usort($breweries, function($a, $b) {
        if ($a['name'] < $b['name']) {
            return -1;
        } elseif ($a['name'] > $b['name']) {
            return 1;
        } else {
            return 0;
        }
    });

    foreach ($aggs['breweries']['buckets'] as $bucket)
    {
        if (isset($breweries[$bucket['key']]))
        {
            $breweries[$bucket['key']]['doc_count'] = $bucket['doc_count'];
        }
    }

    if (true === $filterEmptyOptions)
    {
        foreach ($breweries as $key => $brewery)
        {
            if (0 == $brewery['doc_count'])
            {
                unset($breweries[$key]);
            }
        }
    }

    return $breweries;
}
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Thank you but that doesn't work, does the sort() need to be in the foreach() ? – Will Wright May 25 '17 at 11:12
  • I don't see why you're sorting `$aggs` in the first place. You're returning `$breweries`, and its order doesn't depend on the order of the buckets in `$aggs`. Maybe you should be sorting `$brieweriesObjects`? Can you update the question and show the result you're trying to get? – Barmar May 25 '17 at 11:17
  • Thank you I have updated the question, it is the brewery names that I am trying to sort alphabetically – Will Wright May 25 '17 at 11:20
0

PHP indexes only natural datatypes, you can't execute a function with an argument.

The best case you can do to close the scope is put inside the function or work with anonymous functions to close scope in all your code:

$f = function($data){
    return sort($data);
};
yourfunction($f());
yourfunction2($f());
capcj
  • 1,540
  • 1
  • 13
  • 21